首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

Linux 中字符数组作为全局变量的纠结有关问题

2012-03-03 
Linux 中字符数组作为全局变量的纠结问题小弟最近在做操作系统课设,需要在linux下编程,是关于信号量的,父

Linux 中字符数组作为全局变量的纠结问题
小弟最近在做操作系统课设,需要在linux下编程,是关于信号量的,父进程子进程通信
父进程和子进程通过信号实现异步合作
  在Linux环境下用C语言编程实现,使用用户自定义的信号SIGUSR1由父进程发给子进程,SIGUSR2由子进程发给父进程;父进程和子进程各自从终端接收一个字符串,完成后用kill调用发送信号,接收方接收到信号后,显示对方的进程号及字符串。利用信号方式(Signal(SIGCHLD,SIG_IDN))使父进程不必等待子进程结束,且不产生"ZOMBIE".
 
然后,我需要两个全局变量的字符数组存放输入的字符串,于是
定义了如下的字符串全局变量
#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;
}



现在,最主要的问题是,当我运行的时候发现,主函数中的scanf函数并不起作用,输入并没有改变全局变量字符数组ps,cs,所以在相应函数c_action 和p_action响应时,输出的字符数组仍然是空值,并没有实现相应的功能。。。。按道理说,全局变量的作用域是整个文件啊,scanf应该可以对其修改,但是为什么不起作用呢?想了很长时间,看了很多资料未果?而且本来对linux这东西就有一种畏惧感,所以头晕脑胀,呕吐恶心,只能请求大家帮忙了。。。各路江湖志士,走过路过帮小弟看看吧,时间紧迫,望大家帮忙!!!!1

[解决办法]

C/C++ code
#include <stdio.h>#include <unistd.h>#include <signal.h>#include <sys/mman.h>int pid,ppid;typedef struct{    char ps[100];     char cs[100];}SharedData;SharedData *share_map;void p_action(int signo){  ....}void c_action(int signo){  ....}int main(int argc, char *argv[]){     share_map = (SharedData*)mmap(NULL, sizeof(SharedData),PROT_READ | PROT_WRITE,    MAP_SHARED | MAP_ANONYMOUS, -1, 0);  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",share_map->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",share_map->ps);  printf("parent process %d send signal %d to process %d...\n",getpid(),SIGUSR1,pid);  kill(pid,SIGUSR1);  pause();  break;  }  return 0;} 

热点排行