0
我必須製作程序,這將使兩個子進程。這些進程將在文件中寫入一些東西(字符串...)。父進程應該決定其過程是要寫入文件 我已創建子進程,但我停留在這些信號和我沒有線索如何做到這一點子進程寫入文件
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#define READY_SIGNAL SIGUSR1
#define max 1000
int main(int argc, char *argv[]) {
FILE *file;
int o;
char *name;
opterr = 0;
while ((o = getopt(argc, argv, "hp:")) != -1)
switch (o) {
case 'h':
Help();
exit(1);
default:
exit(1);
}
argc -= optind;
argv += optind;
if(argc==0){
printf("file name\n");
scanf("%s",&name);
file=fopen(name,"a+");
if(file != NULL)
{
printf("file created\n");
// fclose(file);
}
else printf("the file does not exist\n");
}
else if(argc>1) {
return(1);
}
else
meno=argv[0];
file=fopen(name,"a");
if(file != NULL){
printf("file created\n");
}
else printf("the file does not exist\n");
pid_t child_pid, child_pid2;
printf ("the main program process ID is %d\n", (int) getpid());
child_pid = fork() ;
if (child_pid != 0) {
printf ("this is the parent process, with id %d\n", (int) getpid());
printf ("the child's process ID is %d\n",(int) child_pid);
}
else {
printf ("this is the child process, with id %d\n", (int) getpid());
exit(0);
}
child_pid2 = fork() ;
if (child_pid2 != 0) {
printf ("this is the parent process, with id %d\n", (int) getpid());
printf ("the child's process ID is %d\n",(int) child_pid2);
}
else
{
printf ("this is the child process, with id %d\n", (int) getpid());
exit(0);
}
return 0;
}
感謝
如果這是功課,請標記爲這樣。你的問題到底是什麼?你是什麼意思,「卡住這些信號」?你在嘗試什麼,你卡在哪裏? – 2010-12-06 13:48:17