'The last thread Don't Run the task like other Threads in C
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
