1>信号代码
要求:
(1)
源代码:
#include <myhead.h>
void handler(int signo)
{
if (signo == SIGINT)
{
printf("用户按下了ctrl + c键\n");
}
}
int main(int argc, char const *argv[])
{
// //忽略
// if (signal(SIGINT, SIG_IGN) == SIG_ERR)
// {
// perror("signal error");
// return -1;
// }
//默认
// if (signal(SIGINT, SIG_DFL) == SIG_ERR)
// {
// perror("signal error");
// return -1;
// }
//捕获
if (signal(SIGINT, handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
while (1)
{
printf("今天放假\n");
sleep(1);
}
return 0;
}
效果图:
(2)
源代码:
#include <myhead.h>
void handler(int signo)
{
if (SIGCHLD == signo)
{
while (waitpid(-1, NULL, WNOHANG) > 0)
;
}
}
int main(int argc, char const *argv[])
{
if (signal(SIGCHLD, handler) == SIG_ERR)
{
perror("signal error ");
return -1;
}
for (int i = 0; i < 10; i++)
{
if (fork() == 0)
{
sleep(1);
exit(EXIT_SUCCESS);
}
}
while (1)
;
return 0;
}
效果图:
(3)
源代码:
#include<myhead.h>
//定义信号处理函数
void handler(int signo)
{
if(signo == SIGALRM)
{
printf("系统为你随机出了一张牌\n");
alarm(5);
}
}
int main(int argc, const char *argv[])
{
//将SIGALRM信号进行捕获
if(signal(SIGALRM, handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
alarm(5); //启动定时器
char ch = 0;
while(1)
{
scanf("%c", &ch);
getchar();
printf("您出的牌为:%c\n", ch);
alarm(5);
}
return 0;
}
效果图:
(4)
源代码:
#include <myhead.h>
void handler(int signo)
{
if (signo == SIGUSR1)
{
printf("造化弄人呀\n");
raise(SIGKILL);
}
}
int main(int argc, char const *argv[])
{
if (signal(SIGUSR1, handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
pid_t pid = fork();
if (pid > 0)
{
while (1)
{
printf("我真的还想再活五百年\n");
sleep(1);
}
}
else if (pid == 0)
{
sleep(5);
printf("我是子进程,我要独立啦\n");
kill(getppid(), SIGUSR1);
while (1)
{
printf("富贵险中求\n");
sleep(1);
}
}
else
{
perror("fork error");
return -1;
}
return 0;
}
效果图
2>消息队列代码
要求:
源代码:
//msgsnd.c
#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define MAGSIZE sizeof(struct msgbuf) - sizeof(long)
int main(int argc, char const *argv[])
{
key_t key = 0;
if ((key = ftok("/", 'k')) == -1)
{
perror("ftok error");
return -1;
}
int msqid = -1;
if ((msqid = msgget(key, IPC_CREAT | 0664)) == -1)
{
perror("msgget error");
return -1;
}
printf("ftok success key= %#x\n", key);
struct msgbuf sbuf;
while (1)
{
bzero(sbuf.mtext, sizeof(sbuf.mtext));
printf("请输入当前消息的类型:");
scanf("%ld", &sbuf.mtype);
getchar();
printf("请输入消息正文:");
fgets(sbuf.mtext, sizeof(sbuf.mtext), stdin);
sbuf.mtext[strlen(sbuf.mtext) - 1] = 0;
msgsnd(msqid, &sbuf, MAGSIZE,0);
printf("发送成功\n");
if (strcmp(sbuf.mtext, "quit") == 0)
{
break;
}
}
return 0;
}
//msgrcv.c
#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define MSGSIZE sizeof(struct msgbuf) - sizeof(long)
int main(int argc, char const *argv[])
{
key_t key = 0;
if ((key = ftok("/", 'k')) == -1)
{
perror("ftok error");
return -1;
}
printf("ftok success key =%#x\n", key);
int msqid = -1;
if ((msqid = msgget(key, IPC_CREAT | 0664)) == -1)
{
perror("msgget error");
return -1;
}
struct msgbuf rbuf;
while (1)
{
bzero(rbuf.mtext, sizeof(rbuf.mtext));
msgrcv(msqid, &rbuf, MSGSIZE, 1, 0);
printf("收到消息为:%s\n", rbuf.mtext);
if (strcmp(rbuf.mtext, "quit") == 0)
{
break;
}
}
if(msgctl(msqid,IPC_RMID,NULL)==-1)
{
perror("msgctl error");
return -1;
}
return 0;
}
效果图:
3>
要求:
源代码:
//msgsnd.c
#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define MSGSIZE sizeof(struct msgbuf) - sizeof(long)
int main(int argc, char const *argv[])
{
key_t key = 0;
if ((key = ftok("/", 'k')) == -1)
{
perror("ftok error");
return -1;
}
int msqid = -1;
if ((msqid = msgget(key, IPC_CREAT | 0664)) == -1)
{
perror("msgget error");
return -1;
}
printf("ftok success key= %#x\n", key);
struct msgbuf sbuf_r;
pid_t pid = fork();
if (pid > 0)
{
while (1)
{
bzero(sbuf_r.mtext, sizeof(sbuf_r.mtext));
printf("请输入当前消息的类型:\n");
scanf("%ld", &sbuf_r.mtype);
getchar();
printf("请输入消息正文:\n");
fgets(sbuf_r.mtext, sizeof(sbuf_r.mtext), stdin);
sbuf_r.mtext[strlen(sbuf_r.mtext) - 1] = 0;
msgsnd(msqid, &sbuf_r, MSGSIZE, 0);
printf("发送成功\n");
if (strcmp(sbuf_r.mtext, "quit") == 0)
{
break;
}
}
// waitpid(0, NULL, WNOHANG);
wait(NULL);
}
else if (pid == 0)
{
while (1)
{
bzero(sbuf_r.mtext, sizeof(sbuf_r.mtext));
msgrcv(msqid, &sbuf_r, MSGSIZE, 2, 0);
printf("收到消息为:%s\n", sbuf_r.mtext);
if (strcmp(sbuf_r.mtext, "quit") == 0)
{
break;
}
}
if (msgctl(msqid, IPC_RMID, NULL) == -1)
{
perror("msgctl error");
return -1;
}
sleep(2);
exit(EXIT_SUCCESS);
}
else
{
perror("fork error");
return -1;
}
return 0;
}
//msgrcv.c
#include <myhead.h>
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define MSGSIZE sizeof(struct msgbuf) - sizeof(long)
int main(int argc, char const *argv[])
{
key_t key = 0;
if ((key = ftok("/", 'k')) == -1)
{
perror("ftok error");
return -1;
}
printf("ftok success key =%#x\n", key);
int msqid = -1;
if ((msqid = msgget(key, IPC_CREAT | 0664)) == -1)
{
perror("msgget error");
return -1;
}
struct msgbuf rbuf_s;
pid_t pid = fork();
if (pid > 0)
{
while (1)
{
bzero(rbuf_s.mtext, sizeof(rbuf_s.mtext));
msgrcv(msqid, &rbuf_s, MSGSIZE, 1, 0);
printf("收到消息为:%s\n", rbuf_s.mtext);
if (strcmp(rbuf_s.mtext, "quit") == 0)
{
break;
}
}
if (msgctl(msqid, IPC_RMID, NULL) == -1)
{
perror("msgctl error");
return -1;
}
// waitpid(0,NULL,WNOHANG);
wait(NULL);
}
else if (pid == 0)
{
while (1)
{
bzero(rbuf_s.mtext, sizeof(rbuf_s.mtext));
printf("请输入当前消息的类型:\n");
scanf("%ld", &rbuf_s.mtype);
getchar();
printf("请输入消息正文:\n");
fgets(rbuf_s.mtext, sizeof(rbuf_s.mtext), stdin);
rbuf_s.mtext[strlen(rbuf_s.mtext) - 1] = 0;
msgsnd(msqid, &rbuf_s, MSGSIZE, 0);
printf("发送成功\n");
if (strcmp(rbuf_s.mtext, "quit") == 0)
{
break;
}
}
exit(EXIT_SUCCESS);
}
else
{
perror("fork error");
return -1;
}
return 0;
}
效果图: