[英]Create child process, make parent of the child to transmit a file.How do i do this?
This is my code. 这是我的代码。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
int file,parentID,childID;
pid_t pid;
int main(int argc, char *argv[])
{
if( argc != 2 )
{
printf("ERROR ! You have not write an argument\n");
printf("ERROR ! You give more than one argument");
return 1;
}
file = open(argv[1], O_RDONLY); //open file
if(file<0) //test the file
{
printf("Error open file\n");
printf("ERROR : %s\n", strerror(errno));
return 1;
}
// ---------------------------------------------------------------------
pid = fork();
if( pid == -1) //error fork
{
printf("Error fork\n");
return 1;
}
if(pid == 0) // child process
{
childID = getpid();
printf("Child process %d\n",childID);
// if(childID %2 == 1)
// {
// parentID = getppid();
// printf("Process of father of this child= %d\n",parentID);
// }
}
if( pid == 1)
{
parentID = getppid();
printf("ParentProcess %d\n",parentID);
}
}
I have to write a program to create a child process.Depending on the parity of the child process , the parent should transmit to child a message through a file , the message being taken over and showed by the child process( if the child process is a number that is divizible with 2 it will say -"Good morning!" else "Good night!" ).The parent should wait for the final execution of the child to terminate. 我必须编写一个程序来创建一个子进程。根据子进程的奇偶性,父进程应通过文件向子进程发送一条消息,该消息被子进程接管并显示(如果子进程是可以被2整除的数字表示:“早安!”否则“晚安!”)。父母应等待孩子的最终处决终止。
I'm trying really hard to do this exercise and i can't find anywere to explain me how or what function/structure object should i use to do this.Above i tried but i failed , and i understand somehow how fork does but... please help me with this code , or suggest me were should i go to read to make this exercise .Sorry for my bad english spelling. 我真的很努力地做这个练习,我找不到任何可以解释我应该如何或使用哪个功能/结构对象的方法。在上面我尝试了但失败了,但我知道叉子是怎么做的。 ..请帮助我使用此代码,或建议我去阅读该练习。对不起,我的英语拼写错误。
What documentation are you using for the system calls? 您正在使用什么文档进行系统调用?
There are a number of ways to do this, but what you probably want to do is create a pipe, and then fork the process. 有很多方法可以做到这一点,但是您可能想要做的是创建一个管道,然后派生该过程。 Since a fork copies everything, and child processes inherit the environment, each process has a copy of the file descriptors for the pipe.
由于fork复制了所有内容,并且子进程继承了环境,因此每个进程都有管道的文件描述符的副本。 You can then read/write based on the return value of
fork()
. 然后,您可以基于
fork()
的返回值进行读取/写入。
int main(int argc, char *argv[])
{
int fd[2];
char in[128], out[128];
if( argc != 2 )
{
printf("ERROR ! You have not write an argument\n");
printf("ERROR ! You give more than one argument");
return 1;
}
if (pipe(fd) == -1)
return 1;
// ---------------------------------------------------------------------
pid = fork();
if (!pid)
read(fd[0], in, 128);
else
write(fd[1], out, strlen(out) + 1);
note, you usually want to close the file descriptor you're not using for one way communication 请注意,您通常希望关闭不用于单向通信的文件描述符
I think this is the code. 我认为这是代码。
#include <stdio.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void sighandler (int sig)
{
if (sig == SIGUSR1)
{
FILE *f;
char line[100];
f = fopen("file","r");
fgets(line, 100, f);
printf ("Procesul copil cu pid %d a primit mesajul %s", getpid(), line);
fclose(f);
}
}
int main ()
{
pid_t pid;
FILE * f;
pid = fork();
if (pid < 0)
{
perror ("Eroare la fork()");
return (1);
}
else
if (pid == 0)
{
signal (SIGUSR1, sighandler);
pause();
return 0;
}
else
{
if (pid % 2 == 0)
{
printf ("Notificam procesul fiu cu pid %d", pid);
f = fopen ("file","w");
fprintf (f,"Good morning!");
fclose(f);
kill (pid, SIGUSR1);
}
else
{
printf ("Notificam procesul fiu cu pid %d", pid);
f = fopen ("file","w");
fprintf (f,"Good night!");
fclose(f);
kill (pid, SIGUSR1);
}
}
wait(NULL);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.