一、
1.嵌入式Linux多任务:进程、线程
硬件条件:单个CPU单个核
单任务:一个任务执行完毕之后下个任务才能执行;
多任务:任务的执行可以被中断,中断之后可以执行其他任务;重点词汇:(并发/并行)
单核CPU:并发
多核的CPU:既存在并发,又存在并行
2.进程实现多任务
特点:给每个进程分配独立的地址空间
如:4G的大小(1G内核, 3G户空间:栈堆、数据段、代码段);互不干扰;
3.进程创建方式:
- fork > exec函数族> system > vfork;
进程的退出:
- exit()库函数/清理缓冲 ;
- exit()系统调用API/不清理缓冲;
进程等待:
- wait();
解决的问题:僵尸进程
<僵尸进程、孤儿进程、守护进程、控制台进程,后台进程>
二、进程间通信(IPC)
1.
process.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
pid_t pid;
#if 0
int fd[2];
if (pipe(fd) < 0)//创建管道pipe
{
perror("pipe fork error!");
exit(1);
}
#endif
if (mkfifo("/tmp/p1", 0644) < 0)
{
perror("mkfifo error!");
exit(1);
}
pid = fork();
if (pid == -1)
{
perror("fork error!");
exit(1);
}
if (pid == 0)
{
#if 0
//普通文件
int fd = open("b.txt", O_CREAT | O_WRONLY, 0655);
if (fd == -1)
{
perror("open file error!");
exit(1);
}
char buf[100] = "hello world";
write(fd, buf, strlen(buf));
close(fd);
#endif
#if 0
//无名管道
close(fd[0]);
char buf[100];
while (1)
{
scanf("%s", buf);
write(fd[1], buf, strlen(buf));
}
#endif
int fd = open("/tmp/p1", O_WRONLY);
if (fd == -1)
{
perror("open file error!");
exit(1);
}
char buf[100] = {0};
while (1)
{
scanf("%s", buf);
write(fd, buf, strlen(buf));
}
}
else if (pid > 0)
{
#if 0
sleep(1);
int fd = open("b.txt", O_RDONLY);
if (fd == -1)
{
perror("open file error!");
exit(1);
}
char buf[100] = {0};
read(fd, buf, sizeof(buf));
printf("rec buf = %s\n",buf);
close(fd);
#endif
#if 0
char buf[100] = {0};
while (1)
{
int len = read(fd[0], buf, sizeof(buf));
buf[len] = '\0';
printf("recv = %s\n", buf);
}
#endif
int fd = open("/tmp/p1", O_RDONLY);
if (fd == -1)
{
perror("open file error!");
exit(1);
}
char buf[100] = {0};
while (1)
{
int len = read(fd, buf, sizeof(buf));
buf[len] = '\0';
printf("recv = %s\n", buf);
}
}
return 0;
}
读程序read.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
if (mkfifo("/tmp/p1", 0644) < 0)
{
perror("mkfifo error!");
exit(1);
}
int fd = open("/tmp/p1", O_RDONLY);
if (fd == -1)
{
perror("open file error!");
exit(1);
}
char buf[100] = {0};
while (1)
{
int len = read(fd, buf, sizeof(buf));
buf[len] = '\0';
printf("recv = %s\n", buf);
}
return 0;
}
写程序write.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("/tmp/p1", O_WRONLY);
if (fd == -1)
{
perror("open file error!");
exit(1);
}
char buf[100] = {0};
while (1)
{
scanf("%s", buf);
write(fd, buf, strlen(buf));
}
return 0;
}
运行结果:
2.“进程间通信”方式:
①管道
- 无名管道:内核会开辟一个“管道”,通信的进程通过共享这个管道,从而实现通信。int pipe(int pipefd[2]);
特点:
管道只允许具有血缘关系的进程间通信,如父子进程间的通信;管道只允许单向通信;读管道时,如果没有数据的话,读操作会休眠(阻塞),写数据时,缓冲区写满会休眠(阻塞); - 有名管道:管道只允许具有血缘关系的进程间通信,如父子进程间的通信,使用一个“有名管道”是无法实现双向通信的,因为也涉及到抢数据的问题
②消息队列
组成:消息编号;消息正文
struct msgbuf
{
long mtype;/*放消息编号,必须>0*/
char mtext[msgsz];/*消息内容(消息正文)*/
}
特点:传送有格式的消息流;多进程网状套又通信时,消息队列是上上之选;能实现大规模数据的通信;
示例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf
{
/* data */
long id;
char buf[100];
};
int main()
{
pid_t pid;
key_t key;
key = ftok("/tmp/1", 'a');
//msgid消息队列描述符== 文件描述符
int msgid = msgget(key, IPC_CREAT | 0644);
//int msgid = msgget(IPC_PRIVATE, IPC_CREAT | 0644);
if (msgid < 0)
{
perror("msg get error!");
exit(1);
}
pid = fork();
if (pid < 0)
{
perror("fork error!");
exit(1);
}
if (pid == 0)
{ //msgid
struct msgbuf msg;
msg.id = 1;
strcpy(msg.buf, "hello world");
msgsnd(msgid, &msg, sizeof(struct msgbuf), 0);
msg.id = 2;
strcpy(msg.buf, "hello world2");
msgsnd(msgid, &msg, sizeof(struct msgbuf), 0);
msg.id = 3;
strcpy(msg.buf, "hello world3");
msgsnd(msgid, &msg, sizeof(struct msgbuf), 0);
}
else if (pid > 0)
{
//msgid
struct msgbuf msg;
msgrcv(msgid, &msg, sizeof(struct msgbuf), 2, 0);
printf("recv:%s\n", msg.buf);
wait(NULL);
msgctl(msgid,IPC_RMID,NULL);
}
return 0;
}
③共享内存
减少进入内核空间的次数;直接使用地址来读写缓存时,效率会更高, 适用于大数据量的通信;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main()
{
pid_t pid;
key_t key;
key = ftok("/tmp/kk", 'k');
int shmid = shmget(key, 1024, IPC_CREAT | 0644);
if (shmid < 0)
{
perror("shm get error!");
exit(1);
}
pid = fork();
if (pid < 0)
{
perror("fork error!");
exit(1);
}
if (pid == 0)
{//shmid
void *addr = shmat(shmid, NULL, 0);
char buf[100] = {0};
while (1)
{
scanf("%s", buf);
strcpy((char *)addr, buf);
sleep(1);
}
}
else if (pid > 0)
{//shmid
sleep(1);
void *addr = shmat(shmid, NULL, 0);
while (1)
{
if (strlen((char *)addr) > 0)
{
printf("recv = %s\n", (char *)addr);
}
else
{
printf("no data!");
}
sleep(1);
}
}
return 0;
}
④信号量
当多个进程/线程进行共享操作时,用于资源保护,以防止出现相互干扰的情况;信号量其实是OS创建的一个共享变量,进程在进行操作之前,会先检查这个变量的值,这变量的值就是一个标记,通过这个标记就可以知道可不可以操作,以实现互斥;
示例:用信号量保证操作互斥
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int main()
{
pid_t pid;
key_t key = ftok("/tmp/5", 'p');
int semid = semget(key, 1, IPC_CREAT | 0644);
if(semid < 0)
{
perror("sem get error!");
exit(1);
}
if(semctl(semid,0,SETVAL,1) < 0)
{
perror("semctl error!");
exit(1);
}
if ((pid = fork()) < 0)
{
perror("frok error!");
exit(1);
}
//b.txt
if (pid == 0)
{
//"hello world\n"
int fd = open("b.txt", O_WRONLY);
if (fd == -1)
{
perror("open file error!");
exit(1);
}
struct sembuf sem;
sem.sem_num = 0;
sem.sem_flg = SEM_UNDO;
while (1)
{//-1 p
sem.sem_op = -1;
semop(semid,&sem,1);
write(fd, "hello", 5);
write(fd, "world", 5);
write(fd, "\n", 1);
sem.sem_op = +1;
semop(semid,&sem,1);
//v
//+1
//sleep(1);
}
}
else if (pid > 0)
{
//"hhhhh wwwww\n"
int fd = open("b.txt", O_WRONLY);
if (fd == -1)
{
perror("open file error!");
exit(1);
}
struct sembuf sem;
sem.sem_num = 0;
sem.sem_flg = SEM_UNDO;
while (1)
{ //-1
sem.sem_op = -1;
semop(semid,&sem,1);
write(fd, "hhhhh", 5);
write(fd, "wwwww", 5);
write(fd, "\n", 1);
sem.sem_op = +1;
semop(semid,&sem,1);
//+1
//sleep(1);
}
}
return 0;
}