23,125
社区成员
发帖
与我相关
我的任务
分享
# include <unistd.h>
# include <time.h>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
int main(int argc, char *argv[])
{
int iSecret, count;
int pCount= atoi(argv[1]);
pid_t* pid[pCount];
int mtspipe[2], stmpipe[2];
char buf[200], len;
// Create the pipe: master to slave, slave to master.
//If succeed return 0, else -1;
if(pipe(mtspipe))
{
fprintf(stderr,"Create master to slave pipe failed.\n");
return EXIT_FAILURE;
}
if(pipe(stmpipe))
{
fprintf(stderr,"Create slave to master pipe failed.\n");
return EXIT_FAILURE;
}
//Create the child process
if(argc>1)
{
for(count=0;count<pCount;count++)
{
pid[count] = fork();
if(pid[count]==0) //child process
{
close(mtspipe[1]); //close write of mtspipe
close(stmpipe[0]); //close read of stmpipe
//char str[65];
srand(time(NULL));
iSecret = rand()%6+1;
sprintf(buf,"%d:%d",getpid(),iSecret);
write(stmpipe[1],buf,strlen(buf));
printf("This is what we send to master:%s\n",buf);
return EXIT_SUCCESS;
}
else if(pid[count] == -1)
{
printf("Error\n");
}
else // master process
{
close(mtspipe[0]); //close read of mtspipe
close(stmpipe[1]); //close write of stmpipe
len=read(stmpipe[0],buf,sizeof(buf)); //read from stmpipe
printf("This is what we read from slave:%s\n",buf);
sleep(1);
}
}
//wait for all the sub-process to terminate
int i = 0;
for (; i < pCount; ++i) {
int status;
while (-1 == waitpid(pid[i], &status, 0));
if (WIFEXITED(status))
printf("Child exited with code %d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("Child terminated abnormally, signal %d\n", WTERMSIG(status));
}
}
else
{
printf("The command has no other arguments\n");
}
return 0;
}
# include <unistd.h>
# include <time.h>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
int main(int argc, char *argv[])
{
int iSecret, count;
int pCount= atoi(argv[1]);
//pid_t *pid[pCount];
pid_t pid[pCount];
int mtspipe[2], stmpipe[2];
char buf[200], len;
// Create the pipe: master to slave, slave to master.
//If succeed return 0, else -1;
/*if(pipe(mtspipe))
{
fprintf(stderr,"Create master to slave pipe failed.\n");
return EXIT_FAILURE;
}*/
if(pipe(stmpipe))
{
fprintf(stderr,"Create slave to master pipe failed.\n");
return EXIT_FAILURE;
}
//Create the child process
if(argc>1)
{
for(count=0;count<pCount;count++)
{
pid[count] = fork();
if(pid[count]==0) //child process
{
//close(mtspipe[1]); //close write of mtspipe
//close(stmpipe[0]); //close read of stmpipe
//char str[65];
srand(time(NULL));
iSecret = rand()%6+1;
sprintf(buf,"%d:%d",getpid(),iSecret);
write(stmpipe[1],buf,strlen(buf));
printf("This is what we send to master:%s\n",buf);
return EXIT_SUCCESS;
}
else if(pid[count] == -1)
{
printf("Error\n");
}
else // master process
{
//close(mtspipe[0]); //close read of mtspipe
//close(stmpipe[1]); //close write of stmpipe
len=read(stmpipe[0],buf,sizeof(buf)); //read from stmpipe
printf("This is what we read from slave:%s\n",buf);
sleep(1);
}
}
//wait for all the sub-process to terminate
int i = 0;
for (; i < pCount; ++i)
{
int status;
//while (-1 == waitpid(pid[i], &status, 0));
waitpid(pid[i], &status, 0);
if (WIFEXITED(status))
printf("Child exited with code %d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("Child terminated abnormally, signal %d\n", WTERMSIG(status));
}
}
else
{
printf("The command has no other arguments\n");
}
return 0;
}