i need help!!!!!
-
Hi, I know it is a VC++ forum, but i really need help!!! Let's see my code first:
*****************************************************************************
void operation(int[], char[]);
void waitprocess(int);
void main(void)
{
int i, j;
static char buffer[BUFSIZE+10], X_value[5], message[BUFSIZE];
int f_des[2];if (pipe(f\_des) == -1) //create the pipe { perror("Pipe"); exit(2); } if (fork()==0) //In the CHILD { // do nothing here because we will // create 5 CHILDs by PARENT again later. } else //PARENT { for (j=1; j<=5; j++) //create 5 CHILD process { if (fork()==0) { static char temp\[BUFSIZE\]; sprintf(temp, "Thread %d", getpid()); //Get the thread ID operation(f\_des, temp); //Send the request } } while (X>0) // keep doing (read from pipe) until X=0 { close(f\_des\[1\]); if (read(f\_des\[0\], message, BUFSIZE) != -1) //Read from pipe { sprintf(buffer, " - X = %d \\n", X); strcat(message, buffer); write(1, message, sizeof(message)); //display output waitprocess(getpid()); //force sleep awhile X--; } else { printf("error"); } } }
}
****************************************************************************I have to create 5 "threads" but using fork() because i have to use pipe() to communicate with "server". This program is doing that 5 "threads" are trying to decrement the global X until X=0. I use pipe as a mechanisam to protect this "critical section". I don't know whether i did it right or wrong. Please help me to check!!! Also, I found that the output looks strange. The output is always in a pattern. For example, the output looks liked: Thread 1001 - X = 20 Thread 1003 - X = 19 Thread 1002 - X = 18 Thread 1005 - X = 17 Thread 1004 - X = 16 Thread 1001 - X = 15 Thread 1003 - X = 14 Thread 1002 - X = 13 Thread 1005 - X = 12 Thread 1004 - X = 11 * * * You can see the sequence is 1,3,2,5,4,1,3,2,5,4...etc. I think the output should be in random sequence. Why it's happened??? Thanks!!!!!:confused: :-O
-
Hi, I know it is a VC++ forum, but i really need help!!! Let's see my code first:
*****************************************************************************
void operation(int[], char[]);
void waitprocess(int);
void main(void)
{
int i, j;
static char buffer[BUFSIZE+10], X_value[5], message[BUFSIZE];
int f_des[2];if (pipe(f\_des) == -1) //create the pipe { perror("Pipe"); exit(2); } if (fork()==0) //In the CHILD { // do nothing here because we will // create 5 CHILDs by PARENT again later. } else //PARENT { for (j=1; j<=5; j++) //create 5 CHILD process { if (fork()==0) { static char temp\[BUFSIZE\]; sprintf(temp, "Thread %d", getpid()); //Get the thread ID operation(f\_des, temp); //Send the request } } while (X>0) // keep doing (read from pipe) until X=0 { close(f\_des\[1\]); if (read(f\_des\[0\], message, BUFSIZE) != -1) //Read from pipe { sprintf(buffer, " - X = %d \\n", X); strcat(message, buffer); write(1, message, sizeof(message)); //display output waitprocess(getpid()); //force sleep awhile X--; } else { printf("error"); } } }
}
****************************************************************************I have to create 5 "threads" but using fork() because i have to use pipe() to communicate with "server". This program is doing that 5 "threads" are trying to decrement the global X until X=0. I use pipe as a mechanisam to protect this "critical section". I don't know whether i did it right or wrong. Please help me to check!!! Also, I found that the output looks strange. The output is always in a pattern. For example, the output looks liked: Thread 1001 - X = 20 Thread 1003 - X = 19 Thread 1002 - X = 18 Thread 1005 - X = 17 Thread 1004 - X = 16 Thread 1001 - X = 15 Thread 1003 - X = 14 Thread 1002 - X = 13 Thread 1005 - X = 12 Thread 1004 - X = 11 * * * You can see the sequence is 1,3,2,5,4,1,3,2,5,4...etc. I think the output should be in random sequence. Why it's happened??? Thanks!!!!!:confused: :-O
Hahahaha. You're an idiot.
-
Hahahaha. You're an idiot.
David Wengier wrote: Hahahaha. You're an idiot. At first I thought that it was pretty rude. Now I understand what is going on. I think the guy is playing.
-
David Wengier wrote: Hahahaha. You're an idiot. At first I thought that it was pretty rude. Now I understand what is going on. I think the guy is playing.
Rama Krishna wrote: At first I thought that it was pretty rude I think my reply was very rude.