23,121
社区成员
发帖
与我相关
我的任务
分享
#include "apue.h" //一些头文件都放在里面
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILE_PATH "/tmp/10-6Excise.file" //临时文件
static volatile sig_atomic_t sigflag; //进程间的信号标志
static sigset_t newmask, oldmask, zeromask; //屏蔽字
void TELL_WAIT();
void TELL_PARENT(pid_t pid);
void TELL_CHILD(pid_t pid);
void WAIT_PARENT();
void wait_CHILD();
static void sig_usr(int); //自定义信号处理函数
int main()
{
pid_t pid;
int fd;
char buf[128] = {0};
//写0
if((fd = open(FILE_PATH, O_CREAT | O_TRUNC | O_WRONLY)) < 0)
err_sys("open failed");
write(fd, "0", 1);
close(fd);
TELL_WAIT(); //屏蔽自定义的两个信号USR1 USR2
if((pid = fork()) < 0)
{
err_sys("fork error"); //err.c 文件里的自定义错误处理函数
return -1;
}
else if(pid == 0)
{
while(1)
{
WAIT_PARENT();
//子进程增1
sleep(1);
memset(buf, 0, sizeof(buf));
if((fd = open(FILE_PATH, O_RDWR)) < 0)
err_sys("open failed");
read(fd, buf, sizeof(buf));
int i = atoi(buf) + 1;
sprintf(buf, "%d", i);
lseek(fd, 0, SEEK_SET);
write(fd, buf, strlen(buf));
close(fd);
printf("child proc %d write the buf = %s, num = %d\n", getpid(), buf, i);
TELL_PARENT(getppid());
}
}
TELL_CHILD(pid); //先让子进程增1
while(1)
{
WAIT_CHILD();
//父进程增1
sleep(1);
memset(buf, 0, sizeof(buf));
if((fd = open(FILE_PATH, O_RDWR)) < 0)
err_sys("open failed");
read(fd, buf, sizeof(buf));
int i = atoi(buf) + 1;
sprintf(buf, "%d", i);
lseek(fd, 0, SEEK_SET);
write(fd, buf, strlen(buf));
printf("parent proc %d write the buf = %s, num = %d\n", getpid(), buf, i);
close(fd);
TELL_CHILD(pid);
/*
失败可能的原因: 这里发送信号到重新循环
WAIT_CHILD()之间有时间段,还没到再次循环WAIT_CHILD开始,子进程就处理完
了,而且发送完了TELL_PARENT信号,所以信号丢失了, 猜想是这样,所以在WAIT
后处理前加了个sleep, 还是无效,不知道为什么?
*/
}
}
void TELL_WAIT()
{
if(signal(SIGUSR1, sig_usr) == SIG_ERR)
err_sys("signal(SIGUSR1) error");
if(signal(SIGUSR2, sig_usr) == SIG_ERR)
err_sys("signal(SIGUSR2) error");
sigemptyset(&newmask);
sigemptyset(&zeromask);
sigaddset(&newmask, SIGUSR1);
sigaddset(&newmask, SIGUSR2);
if(sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
err_sys("SIG_BLOCK error");
}
void TELL_PARENT(pid_t pid)
{
kill(pid, SIGUSR1);
}
void TELL_CHILD(pid_t pid)
{
kill(pid, SIGUSR2);
}
void WAIT_PARENT()
{
while(sigflag == 0)
sigsuspend(&zeromask);
sigflag = 0;
if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_BLOCK error");
}
void WAIT_CHILD()
{
while(sigflag == 0)
sigsuspend(&zeromask);
sigflag = 0;
if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_BLOCK error");
}
static void sig_usr(int signo)
{
sigflag = 1;
}
sigprocmask// 这里要锁信号
while(sigflag == 0)
sigsuspend(&zeromask);
sigflag = 0;
if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) //这里解信号
err_sys("SIG_BLOCK error");
#include "apue.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILE_PATH "/tmp/10-6Excise.file"
static volatile sig_atomic_t sigflag;
static sigset_t newmask, oldmask, zeromask;
void TELL_WAIT();
void TELL_PARENT(pid_t pid);
void TELL_CHILD(pid_t pid);
void WAIT_PARENT();
void WAIT_CHILD();
static void sig_usr(int);
int main()
{
pid_t pid;
int fd;
char buf[128] = {0};
if((fd = open(FILE_PATH, O_CREAT | O_TRUNC | O_WRONLY)) < 0)
err_sys("open failed");
write(fd, "0", 1);
close(fd);
TELL_WAIT();
if((pid = fork()) < 0)
{
err_sys("fork error");
return -1;
}
else if(pid == 0)
{
while(1)
{
printf("waite parent\n");
WAIT_PARENT();
sleep(1);
memset(buf, 0, sizeof(buf));
if((fd = open(FILE_PATH, O_RDWR)) < 0)
err_sys("open failed");
read(fd, buf, sizeof(buf));
int i = atoi(buf) + 1;
sprintf(buf, "%d", i);
lseek(fd, 0, SEEK_SET);
write(fd, buf, strlen(buf));
close(fd);
printf("child proc %d write the buf = %s, num = %d\n", getpid(), buf, i);
TELL_PARENT(getppid());
printf("tell parent\n");
}
}
TELL_CHILD(pid);
while(1)
{
printf("wait child\n");
WAIT_CHILD();
sleep(1);
memset(buf, 0, sizeof(buf));
if((fd = open(FILE_PATH, O_RDWR)) < 0)
err_sys("open failed");
read(fd, buf, sizeof(buf));
int i = atoi(buf) + 1;
sprintf(buf, "%d", i);
lseek(fd, 0, SEEK_SET);
write(fd, buf, strlen(buf));
printf("parent proc %d write the buf = %s, num = %d\n", getpid(), buf, i);
close(fd);
TELL_CHILD(pid);
printf("tell child\n");
}
}
void TELL_WAIT()
{
if(signal(SIGUSR1, sig_usr) == SIG_ERR)
err_sys("signal(SIGUSR1) error");
if(signal(SIGUSR2, sig_usr) == SIG_ERR)
err_sys("signal(SIGUSR2) error");
sigemptyset(&newmask);
sigemptyset(&zeromask);
sigaddset(&newmask, SIGUSR1);
sigaddset(&newmask, SIGUSR2);
if(sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
err_sys("SIG_BLOCK error");
}
void TELL_PARENT(pid_t pid)
{
kill(pid, SIGUSR1);
}
void TELL_CHILD(pid_t pid)
{
kill(pid, SIGUSR2);
}
void WAIT_PARENT()
{
while(sigflag == 0)
sigsuspend(&zeromask);
sigflag = 0;
//if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
// err_sys("SIG_BLOCK error");
}
void WAIT_CHILD()
{
while(sigflag == 0)
sigsuspend(&zeromask);
sigflag = 0;
//if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
// err_sys("SIG_BLOCK error");
}
static void sig_usr(int signo)
{
sigflag = 1;
}
root@ywj-virtual-machine:/codeTest# gcc 10-6Excise.c err.c -o 10-6Excise
root@ywj-virtual-machine:/codeTest# ./10-6Excise
wait child
waite parent
child proc 31995 write the buf = 1, num = 1
tell parent
waite parent
parent proc 31994 write the buf = 2, num = 2
tell child
wait child
owenliang@linux-7lsl:~/csdn/src> gcc -o main main.c
owenliang@linux-7lsl:~/csdn/src> ./main
child proc 7796 write the buf = 1, num = 1
parent proc 7795 write the buf = 2, num = 2
child proc 7796 write the buf = 3, num = 3
parent proc 7795 write the buf = 4, num = 4
child proc 7796 write the buf = 5, num = 5
parent proc 7795 write the buf = 6, num = 6
^C
owenliang@linux-7lsl:~/csdn/src> cat main.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_PATH "/tmp/10-6Excise.file" //临时文件
static volatile sig_atomic_t sigflag; //进程间的信号标志
static sigset_t newmask, oldmask, zeromask; //屏蔽字
void TELL_WAIT();
void TELL_PARENT(pid_t pid);
void TELL_CHILD(pid_t pid);
void WAIT_PARENT();
void WAIT_CHILD();
static void sig_usr(int); //自定义信号处理函数
int main()
{
pid_t pid;
int fd;
char buf[128] = {0};
//写0
if((fd = open(FILE_PATH, O_CREAT | O_TRUNC | O_WRONLY)) < 0)
{}
write(fd, "0", 1);
close(fd);
TELL_WAIT(); //屏蔽自定义的两个信号USR1 USR2
if((pid = fork()) < 0)
{
return -1;
}
else if(pid == 0)
{
while(1)
{
WAIT_PARENT();
//子进程增1
sleep(1);
memset(buf, 0, sizeof(buf));
if((fd = open(FILE_PATH, O_RDWR)) < 0)
;
read(fd, buf, sizeof(buf));
int i = atoi(buf) + 1;
sprintf(buf, "%d", i);
lseek(fd, 0, SEEK_SET);
write(fd, buf, strlen(buf));
close(fd);
printf("child proc %d write the buf = %s, num = %d\n", getpid(), buf, i);
TELL_PARENT(getppid());
}
}
TELL_CHILD(pid); //先让子进程增1
while(1)
{
WAIT_CHILD();
//父进程增1
sleep(1);
memset(buf, 0, sizeof(buf));
if((fd = open(FILE_PATH, O_RDWR)) < 0)
;
read(fd, buf, sizeof(buf));
int i = atoi(buf) + 1;
sprintf(buf, "%d", i);
lseek(fd, 0, SEEK_SET);
write(fd, buf, strlen(buf));
printf("parent proc %d write the buf = %s, num = %d\n", getpid(), buf, i);
close(fd);
TELL_CHILD(pid);
/*
失败可能的原因: 这里发送信号到重新循环
WAIT_CHILD()之间有时间段,还没到再次循环WAIT_CHILD开始,子进程就处理完
了,而且发送完了TELL_PARENT信号,所以信号丢失了, 猜想是这样,所以在WAIT
后处理前加了个sleep, 还是无效,不知道为什么?
*/
}
}
void TELL_WAIT()
{
if(signal(SIGUSR1, sig_usr) == SIG_ERR)
;
if(signal(SIGUSR2, sig_usr) == SIG_ERR)
;
sigemptyset(&newmask);
sigemptyset(&zeromask);
sigaddset(&newmask, SIGUSR1);
sigaddset(&newmask, SIGUSR2);
if(sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
;
}
void TELL_PARENT(pid_t pid)
{
kill(pid, SIGUSR1);
}
void TELL_CHILD(pid_t pid)
{
kill(pid, SIGUSR2);
}
void WAIT_PARENT()
{
while(sigflag == 0)
sigsuspend(&zeromask);
sigflag = 0;
/*
if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_BLOCK error");
*/
}
void WAIT_CHILD()
{
while(sigflag == 0)
sigsuspend(&zeromask);
sigflag = 0;
/*
if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_BLOCK error");
*/
}
static void sig_usr(int signo)
{
sigflag = 1;
}