linux环境下父进程子进程异步问题

damahayucalifornia 2011-12-31 08:42:29
各位大侠,小弟学完操作系统,感觉学的也不差啊,但现在老师让做课程设计,题目是在Linux环境下编程,然后就焦虑了。之前学的时候也有看过一些linux内核方面的书,但看的云里雾里的,坚持了一段时间后由于其他课程太多,也就放了下来,结果现在就悲剧了,虽然借了好几本书狂翻资料,但还是感觉有点摸不到头绪,我觉的这东西,要是对linux内核机制有一定的了解的话还是很容易的,但现在关键是没有那么多时间让我去看,恐怕到时不能完成,所以请大家帮我指点指点该如何做,如果能给出相应的源程序当然更好!到时分一定送上;
下面是我的题目:
父进程和子进程通过信号实现异步合作
在Linux环境下用C语言编程实现,使用用户自定义的信号SIGUSR1由父进程发给子进程,SIGUSR2由子进程发给父进程;父进程和子进程各自从终端接收一个字符串,完成后用kill调用发送信号,接收方接收到信号后,显示对方的进程号及字符串。利用信号方式(Signal(SIGCHLD,SIG_IDN))使父进程不必等待子进程结束,且不产生"ZOMBIE".
拜托了!
...全文
337 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Richardkaola 2012-01-01
  • 打赏
  • 举报
回复
fork()得到一个子进程。
程序先进入 父进程的default ,遇到 sleep 阻塞后进入到 子进程的 case 0 同样sleep阻塞回到 父进程的default(我认为你这里的sleep没意义)。接下来 scanf IO 阻塞,但另一边也还阻塞着(就算sleep阻塞结束接下来又有一个scanf 的 IO 阻塞)
然后 现在你输入的 信息 会输入到 父进程的 ps 中。然后你给子进程发送了一个信号:kill(pid,SIGUSR1); 子进程收到信号后调用了 c_action 。不过 多进程的地址空间是独立的,子进程里的ps没有定义, 所以应该会输出 空串。同理 子进程的 cs 虽然你赋值了,但是 父进程执行 信号处理函数时,是调用自己地址空间里的cs,自然也应该是空串。
damahayucalifornia 2011-12-31
  • 打赏
  • 举报
回复
题目要求完成输入字符串发送,接收方接收到并予以相应(输出发送方的pid和字符串“),而且显示的是发送方的进程号,不是接受方的进程号
那按照1楼的说法我是否可以定义两个全局变量char ps[100]; char cs[100];而且把pid和ppid也定义成全局变量;
然后再做一下修改:
case:0:
....
printf("please input the string from child:\n");
scanf("%s",cs);
kill(ppid,SIGUSR2);
break;
default:
....
printf("please input the string from parent:\n");
scanf("%s",ps);
kill(pid,SIGUSR1);
...
}
然后再对p_action和c_action修改
void p_action(int sig)
{
printf("parent process %d recieve SIGUSR2 signal,signo is %d\n",getpid(),signo);
printf("the sender(son) process's pid is %d\n",pid);
printf("the chararray from child process is:%s\n",cs);
}
void c_action(int sig)
{
printf("child process %d recieve SIGUSR1 signal,signo is %d\n",getpid(),signo);
printf("the sender(parent) process's pid is %d\n",ppid);
printf("the chararray from parent process is:%s\n",ps);
}
高人们快来指点指点啊
wesleyluo 2011-12-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mscf 的回复:]

C/C++ code

#include <stdio.h>
#include <unistd.h>
#include <signal.h>


void p_action(int signo){
printf("parent process %d recieve SIGUSR2 signal,signo is %d\n",getpid(),signo);
}

void c_acti……
[/Quote]
+
薛定谔之死猫 2011-12-31
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <unistd.h>
#include <signal.h>


void p_action(int signo){
printf("parent process %d recieve SIGUSR2 signal,signo is %d\n",getpid(),signo);
}

void c_action(int signo){
printf("child process %d recieve SIGUSR1 signal,signo is %d\n",getpid(),signo);
}

int main(int argc, char *argv[])
{
int pid,ppid;
signal(SIGCHLD,SIG_IGN);
switch(pid=fork()){
case -1:
printf("create child process error!\n");
break;
case 0:
signal(SIGUSR1,c_action);
ppid=getppid();
sleep(5);
printf("child process %d send signal %d to process %d...\n",getpid(),SIGUSR2,ppid);
kill(ppid,SIGUSR2);
break;
default:
signal(SIGUSR2,p_action);
sleep(2);
printf("parent process %d send signal %d to process %d...\n",getpid(),SIGUSR1,pid);
kill(pid,SIGUSR1);
pause();
break;
}
return 0;
}
damahayucalifornia 2011-12-31
  • 打赏
  • 举报
回复

大家抽点时间帮小弟看下,确实是费解,头疼,我把楼上的程序按要求修改以后发现一个问题,我定义的两个字符串全局变量ps和cs再main函数中用scanf函数不能输入,不起作用。。。。按理说,全局变量应该是在整个文件内有效啊,但是,为什么,为什么,为什么,scanf函数不起作用?????查了很多资料未果,,心里很是纠结。。。。大家帮忙看看吧 如果不设全局变量,那又该如何在接收方的函数中显示发送方输入的字符串呢????
下面是我修改后的源程序
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int pid,ppid;
char ps[100];
char cs[100];
void p_action(int signo){
printf("parent process %d recieve SIGUSR2 signal,signo is %d\n",getpid(),signo);
printf("the sender(child) process's pid is %d\n",pid);
printf("the chararray from child process is:%s\n",cs);
}

void c_action(int signo){
printf("child process %d recieve SIGUSR1 signal,signo is %d\n",getpid(),signo);
printf("the sender(parent) process's pid is %d\n",ppid);
printf("the chararray from parent process is:%s\n",ps);
}

int main(int argc, char *argv[])
{

signal(SIGCHLD,SIG_IGN);
switch(pid=fork()){
case -1:
printf("create child process error!\n");
break;
case 0:
signal(SIGUSR1,c_action);
ppid=getppid();

sleep(5);
printf("please input the string from child:\n");
scanf("%s",cs);
printf("child process %d send signal %d to process %d...\n",getpid(),SIGUSR2,ppid);
kill(ppid,SIGUSR2);
break;
default:
signal(SIGUSR2,p_action);

sleep(2);
printf("please input the string from parent:\n");
scanf("%s",ps);
printf("parent process %d send signal %d to process %d...\n",getpid(),SIGUSR1,pid);
kill(pid,SIGUSR1);
pause();
break;
}
return 0;
}
damahayucalifornia 2011-12-31
  • 打赏
  • 举报
回复
是,这个明白,signal(SIGCHLD,SIG_IGN);是消除僵死进程。但我想知道的是,我那样改那个程序可否?
qq120848369 2011-12-31
  • 打赏
  • 举报
回复

POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)), then
children that terminate do not become zombies and a call to wait() or waitpid() will block until all children have terminated, and then fail with
errno set to ECHILD. (The original POSIX standard left the behavior of setting SIGCHLD to SIG_IGN unspecified. Note that even though the default
disposition of SIGCHLD is "ignore", explicitly setting the disposition to SIG_IGN results in different treatment of zombie process children.)
Linux 2.6 conforms to this specification. However, Linux 2.4 (and earlier) does not: if a wait() or waitpid() call is made while SIGCHLD is being
ignored, the call behaves just as though SIGCHLD were not being ignored, that is, the call blocks until the next child terminates and then returns
the process ID and status of that child.



看完这个应该都懂了。

18,772

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧