子进程写入文件
我必须编写程序,这将创建两个子进程。这些进程将在文件中写入一些内容(字符串...)。父进程应该决定哪个进程要写入文件 我已经创建了子进程,但我被这些信号困住了,我不知道如何做到这一点,
#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;
}
谢谢
i have to make program, which will make two children processes. These processes will write something(string...) in file. Parent process should decide which process is going to write to file
i have created children processes, but i am stuck at these signals and i don't have a clue how to do this
#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;
}
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的子进程在创建后就会立即退出。如果他们不这样做,那么第一个孩子就会创建自己的孩子。您可能想在 for 循环中创建子级并执行以下操作:
在 fork() 之前打开文件是一个坏主意。您最终将得到三个进程,它们都持有具有相同权限的同一文件的文件句柄。如果你这样做,生活就会变得复杂!一般来说,仅在确实需要时才打开文件,并在使用完毕后立即关闭它们。
我认为你的问题的意思是你希望父进程通过从父进程向子进程发送信号来告诉子进程写入。有更简单的方法可以做到这一点,但我想你的老师希望你演示如何使用信号来做到这一点。
首先,您需要编写一个信号处理程序。有关详细信息,请参阅 http://linux.die.net/man/2/signal关于如何做到这一点。
其次,您需要实际发送信号。有关详细信息,请参阅 http://linux.die.net/man/2/kill 。请注意,“kill”这个名字有点用词不当。
To start with your child processes are exiting as soon as they are created. If they didn't then the first child would then create a child of it's own. You probably want to create the children in a for loop and do something like:
It is a bad idea to open the file before you fork(). You will end up with three processes who all hold a file handle to the same file with the same permissions. Life starts getting complicated if you do that! In general only open files when you really need to, and close them as soon as you have finished using them.
I think what your question means is that you want the parent process to tell the child to write by sending a signal from the parent to the child. There are easier ways of doing this, but I guess your teacher wants you to demonstrate how to do it with signals.
First off, you need to write a signal handler. See http://linux.die.net/man/2/signal for more information on how to do this.
Second you need to actually send the signal. See http://linux.die.net/man/2/kill for more information. Note that the name "kill" is a bit of a misnomer.