最后一个线程不像C中的其他线程一样运行任务

发布于 2025-01-30 03:25:19 字数 3743 浏览 2 评论 0原文

因此,在主要功能中,我定义了用于创建变量的变量。 同样的过程。 然后,我将父程进程,然后在子进程中,我创建了4个线程,并且在主函数上方创建了线程将运行的函数。 我的问题是前3个线程完全按照我的意愿工作,但是第四个过程不作为前3个线程。

这是我执行子过程的代码。

我使用普通管道写出来自父过程的消息,并从子进程中读取消息。 读取器代表我从管道中读取的数据和大约150个字符长度。

    #include <pthread.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/wait.h>
    #include <signal.h>
    
    
    #ifndef NUM_THREADS
    #define NUM_THREADS 4
    #endif


    [const char readMessage\[152\];][1]


    void *encrypt(char *data)
    {
        int shift = 4;

        int i = 0;

        for(i = 0; data[i] != '\0' ; ++i) {
            char sm = data[i];
            if(sm> 'a' && sm<='z'){
                sm = sm + shift;
                if(sm>'z'){
                    sm = sm - 'z' + 'a' - 1;                 
                  }
                data[i] = sm;                          
            }
                 else if(sm >= 'A' && sm<='Z'){
                        sm = sm + shift;
                        if(sm > 'Z'){
                            sm = sm - 'Z' + 'A'-1;                            
                          }
                        data[i] = sm;            
                    }         
        }            
        
        return data;
     }

    void *threadFunc(void *message) {
            printf("encrypted data : %s\n", encrypt(message));
    
        return encrypt(message);    
        }

int main(void)
{
    pthread_t threads[NUM_THREADS];           

    int pipefds[2];     
        
    //pipefds[0] - READ
    //pipefds[1] - WRİTE
    
    int returnstatus;  
    
    char readBUFFER[152];

        
    int pid;

        
    char writeMessages[152]={"150 characters is between 20 words and 40 words with spaces included in the character count. If spaces are not included in the character count,then 150 "};
    
    returnstatus = pipe(pipefds);
    if(returnstatus == -1)
    {
        printf("Pipe failed!\n");
        return 1;    
    }
    
    pid = fork();  //Child Process Oluşturma

    if(pid == -1)
    {
        printf("Fork failed!\n");
        return 2;    
    }
        
    //Child Process
    if(pid == 0)
    {
        close(pipefds[1]); 

        read(pipefds[0], readBUFFER , sizeof(readBUFFER));

        printf("Child process Reads from pipe -->  :%s\n",readBUFFER);

        int buffer_size = strlen(readBUFFER);

        printf("buffer size : %d\n",buffer_size);
        

        char str[50];
            
        for(int i = 1; i<=NUM_THREADS ;i++)
        {
            for(int j = ((buffer_size/4) * (i - 1))  ;  j<=((buffer_size/4) * i) ;  j++)
            {   
                 strncat(str , &readBUFFER[j] , 1);   
            }    
            printf("%d. Part --> %s\n", i,str);
                            

            pthread_create(&threads[i] , NULL , threadFunc , str);
            sleep(2);
            

            int pos = 0;
            

            while(str[pos] != '\0')
            {
              str[pos] = '\0';
            }

            //pthread_join(threads[i] , NULL);
        }    
        
        sleep(2);
    }
    
    //Parent process
    else  
    {
        close(pipefds[0]);
    printf("parent process write to the pipe -->\n");
    write(pipefds[1], writeMessages , sizeof(writeMessages));
    wait(NULL);
    printf("Child process Executed!");
    }

    return 0;
}

基本上,加密函数执行凯撒加密。

我的问题是,当我运行第一个线程时,他们会返回加密的消息,但是当涉及第四个线程时,它没有返回。

我无法解决这个问题,也许是因为我还没有掌握线程。

让我知道您是否需要更多信息。

这是我接受的输出。看看四部分。它代表第四个线程。

So in main function I have defined variable for creating variable.
Same thing for process.
Then I fork() the parent process and then inside the child process I have created 4 Threads and above the Main function ı have created the functions Where the Threads will run.
My question is The first 3 Thread works exactly as I want But the Fourth process not working as the First 3 Thread.

Here is my code that Child Process Execute.

I used Ordinary pipe to Write message from Parent Process and Read the Message from Child Process.
readBUFFER stands for the data I read from the Pipe and about 150 character length.

    #include <pthread.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/wait.h>
    #include <signal.h>
    
    
    #ifndef NUM_THREADS
    #define NUM_THREADS 4
    #endif


    [const char readMessage\[152\];][1]


    void *encrypt(char *data)
    {
        int shift = 4;

        int i = 0;

        for(i = 0; data[i] != '\0' ; ++i) {
            char sm = data[i];
            if(sm> 'a' && sm<='z'){
                sm = sm + shift;
                if(sm>'z'){
                    sm = sm - 'z' + 'a' - 1;                 
                  }
                data[i] = sm;                          
            }
                 else if(sm >= 'A' && sm<='Z'){
                        sm = sm + shift;
                        if(sm > 'Z'){
                            sm = sm - 'Z' + 'A'-1;                            
                          }
                        data[i] = sm;            
                    }         
        }            
        
        return data;
     }

    void *threadFunc(void *message) {
            printf("encrypted data : %s\n", encrypt(message));
    
        return encrypt(message);    
        }

int main(void)
{
    pthread_t threads[NUM_THREADS];           

    int pipefds[2];     
        
    //pipefds[0] - READ
    //pipefds[1] - WRİTE
    
    int returnstatus;  
    
    char readBUFFER[152];

        
    int pid;

        
    char writeMessages[152]={"150 characters is between 20 words and 40 words with spaces included in the character count. If spaces are not included in the character count,then 150 "};
    
    returnstatus = pipe(pipefds);
    if(returnstatus == -1)
    {
        printf("Pipe failed!\n");
        return 1;    
    }
    
    pid = fork();  //Child Process Oluşturma

    if(pid == -1)
    {
        printf("Fork failed!\n");
        return 2;    
    }
        
    //Child Process
    if(pid == 0)
    {
        close(pipefds[1]); 

        read(pipefds[0], readBUFFER , sizeof(readBUFFER));

        printf("Child process Reads from pipe -->  :%s\n",readBUFFER);

        int buffer_size = strlen(readBUFFER);

        printf("buffer size : %d\n",buffer_size);
        

        char str[50];
            
        for(int i = 1; i<=NUM_THREADS ;i++)
        {
            for(int j = ((buffer_size/4) * (i - 1))  ;  j<=((buffer_size/4) * i) ;  j++)
            {   
                 strncat(str , &readBUFFER[j] , 1);   
            }    
            printf("%d. Part --> %s\n", i,str);
                            

            pthread_create(&threads[i] , NULL , threadFunc , str);
            sleep(2);
            

            int pos = 0;
            

            while(str[pos] != '\0')
            {
              str[pos] = '\0';
            }

            //pthread_join(threads[i] , NULL);
        }    
        
        sleep(2);
    }
    
    //Parent process
    else  
    {
        close(pipefds[0]);
    printf("parent process write to the pipe -->\n");
    write(pipefds[1], writeMessages , sizeof(writeMessages));
    wait(NULL);
    printf("Child process Executed!");
    }

    return 0;
}

Basically encrypt function do the Caesar Encryption.

My question is when I run the First Thread they return the encrypted message but when it comes to the Fourth Thread it returns nothing.

I couldn't solve this problem maybe it's because ı haven't yet had mastered threads.

Let me know if you need more information.

and this is the output I took. Look at the 4 part. It represent the Fourth Thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文