一、进程
1、进程的基本概念
进程:程序的一次动态执行过程,包括创建、调度、消亡
进程是程序执行和资源管理的最小单位,
进程种类:交互进程、批处理进程、守护进程
进程状态:运行态、等待态(可中断/不可中断)、僵尸态(已终止的进程,但还在进程向量表中占一个task_struct)、停止态
2、进程的操作
创建:pid_t fork(void)
父进程中fork的返回值是子进程的pid;子进程中fork的返回值为0
退出:exit( int status) 和 _exit(int status ),子函数中调exit只是该进程结束,调return只是这个函数结束,进程并没有结束。
等待:
1、阻塞函数直到进程退出,wait返回,子进程被回收
pid_t wait(int *status)
2、可以指定等待某个子进程结束以及等待的方式(阻塞或非阻塞),
子进程退出返回子进程pid,使用选项WNOHANG且没有子进程结束时0
pid_t waitpid(pid_t pid,int *status,int options)
替换代码段:int execl(const char* path,const char*arg,)程序路径、命令行参数
3、守护进程
(1)创建子进程,父进程退出
(2)子进程中创建新会话pid_t setsid()
(3)改变当前目录为根目录chdir("/")
(4)重设文件权限掩码umask(0)
(5)关闭文件描述符int getdtablesize();获取最大文件描述符
4、进程间通信
无名管道、有名管道、信号、共享内存、消息队列、信号灯、套接字
(1)无名管道:
半双工,只能用于父子进程、兄弟进程间通信
int pipe(int fd[2])
arr[0]读管道、arr[1]写管道
(2)有名管道:
管道文件test.fifo,为了两个互不相关的进程能够操作同一个管道
int mkfifo(const char*filename,mode_t mode) 管道文件的路径 管道文件的读写权限0666
(3)信号
信号注册函数 :void (* signal(int signum , void(*handler)(int)) ) (int)
SIG_IGN:忽略该信号
SIG_DFL:用系统默认方式处理信号
自定义的信号处理函数指针
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
void sigFunc(int signum)
{
if(SIGCHLD == signum)
{
int ret = waitpid(-1, NULL, WNOHANG);
if(0 == ret)
{
printf("子进程状态发生改变但没结束!\n");
}
else if(ret > 0)
{
printf("子进程结束已被回收!\n");
}
}
}
int main()
{
printf("11111111111111\n");
pid_t pid = fork();
if(pid < 0)
{
perror("fork error!");
return -1;
}
else if(pid > 0)
{
//注册信号
//子进程状态改变时,父进程就会收到SIGCHLD这个信号
signal(SIGCHLD, sigFunc);
//父进程中fork的返回值是子进程的pid
//父进程会进这个分支
while(1)
{
printf("I am parent process!, pid = %d\n", getpid());
sleep(1);
}
}
else
{
//子进程中的fork的返回值为0
//子进程会进这个分支
int i = 0;
while(1)
{
i++;
printf("I am child process!pid = %d, ppid = %d\n", getpid(), getppid());
sleep(1);
if(20 == i)
{
printf("kkkkkkk");
//return 0; main函数return
_exit(5);
}
}
}
while(1);
return 0;
}
(4)共享内存
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main()
{
//创建并打开共享内存
key_t key = ftok(".", 10);
if(key < 0)
{
perror("ftok error!");
return -1;
}
printf("key = %d\n", key);
int shmId = shmget(key, 100, 0666 | IPC_CREAT);
if(shmId < 0)
{
perror("shmget error!");
return -1;
}
printf("shmId = %d\n", shmId);
//将用户空间的虚拟内存映射到内核空间的物理内存
void *p = shmat(shmId, NULL, 0);
if((void *)-1 == p)
{
perror("shmat error!");
//删除共享内存
shmctl(shmId, IPC_RMID, NULL);
return 0;
}
//操作共享内存
printf("input int:");
scanf("%d", (int *)p);
//取消映射
shmdt(p);
//删除共享内存
shmctl(shmId, IPC_RMID, NULL);
return 0;
}
(5)消息队列
是一个消息的列表,用户可以在消息队列中添加消息、读取消息;由消息队列ID来唯一标识,可以按照类型来发送/接收消息
write:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#define N 100
typedef struct msg
{
long mtype;
char text[N];
}data_t;
int main()
{
//创建并打开消息队列
key_t key = ftok(".", 15);
if(key < 0)
{
perror("ftok error!");
return -1;
}
printf("key = %d\n", key);
int msgId = msgget(key, 0666 | IPC_CREAT);
if(msgId < 0)
{
perror("msgget error!");
return -1;
}
printf("msgId = %d\n", msgId);
data_t data = {0};
int i;
for(i = 0; i < 5; i++)
{
memset(&data, 0, sizeof(data));
printf("mtype:");
scanf("%ld", &data.mtype);
printf("msg:");
scanf("%s", data.text);
//添加消息
int ret = msgsnd(msgId, (const void *)&data, N, 0);
if(ret < 0)
{
perror("msgsnd error!");
msgctl(msgId, IPC_RMID, NULL);
return -1;
}
}
return 0;
}
read:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#define N 100
typedef struct msg
{
long mtype;
char text[N];
}data_t;
int main()
{
//创建并打开消息队列
key_t key = ftok(".", 15);
if(key < 0)
{
perror("ftok error!");
return -1;
}
printf("key = %d\n", key);
int msgId = msgget(key, 0666 | IPC_CREAT);
if(msgId < 0)
{
perror("msgget error!");
return -1;
}
//读消息
data_t data = {0};
int ret = msgrcv(msgId, &data, sizeof(data.text), 3, 0);
if(ret < 0)
{
perror("msgrcv error!");
msgctl(msgId, IPC_RMID, NULL);
return -1;
}
printf("mtype:%d, msg:%s\n", data.mtype, data.text);
//删除消息队列
//msgctl(msgId, IPC_RMID, NULL);
return 0;
}
(6)套接字
二、线程
线程是系统调度的最小单位,线程就是轻量级的进程,对于OS而言进程与线程都是需要调度的任务,而且都在内核空间都有task_struct,线程共享进程的地址空间,不需要来回切换地址空间,开销更小
1、线程的创建与回收
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
char buf[] = "hello world!";
//线程执行的函数
void *threadFunc1(void *arg)
{
int i = 0;
while(1)
{
printf("thread1.......%s\n", buf);
sleep(1);
i++;
if(5 == i)
{
//线程退出
pthread_exit(buf);//内核中还有task_struct
//return buf; //函数结束线程就结束
}
}
return NULL;
}
void *threadFunc2(void *arg)
{
while(1)
{
printf("thread2.......%s\n", buf);
sleep(1);
}
}
int main()
{
//主线程
//创建子线程
pthread_t thId1, thId2;
pthread_create(&thId1, NULL, threadFunc1, NULL);
pthread_create(&thId2, NULL, threadFunc2, NULL);
//void *th1_val = NULL;
//pthread_join(thId1, &th1_val);
//printf("th1 quit,return_val = %p\n", th1_val);
//将子线程设置为游离态,分离
//子线程退出,系统会自动回收子线程
pthread_detach(thId1);
pthread_detach(thId2);
while(1)
{
printf("process......%s\n", buf);
sleep(1);
}
return 0;
}
2、线程的同步:信号量
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
sem_t sem1, sem2;
//线程执行的函数
void *getPicThread(void *arg)
{
while(1)
{
//申请sem1的资源
sem_wait(&sem1);
printf("正在采集图片....\n");
usleep(200);
//释放sem2的资源
sem_post(&sem2);
}
return NULL;
}
void *handlePicThread(void *arg)
{
while(1)
{
//申请sem2的资源
sem_wait(&sem2);
printf("正在转换图片\n");
usleep(100);
//释放sem1的资源
sem_post(&sem1);
}
}
int main()
{
//初始化信号量
sem_init(&sem1, 0, 3);
sem_init(&sem2, 0, 0);
//主线程
//创建子线程
pthread_t thId1, thId2;
pthread_create(&thId1, NULL, getPicThread, NULL);
pthread_create(&thId2, NULL, handlePicThread, NULL);
pthread_detach(thId1);
pthread_detach(thId2);
while(1);
return 0;
}
3、线程的互斥:互斥锁
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
//共享资源
char buf[100] = {0};
void *threadFunc1(void *arg)
{
char *p = (char *)arg;
int i = 0;
//加锁
pthread_mutex_lock(&mutex);
while(p[i] != '\0')
{
buf[i] = p[i];
usleep(200);
i++;
}
printf("th1 buf:%s\n", buf);
//解锁
pthread_mutex_unlock(&mutex);
}
void *threadFunc2(void *arg)
{
char *p = (char *)arg;
int i = 0;
//加锁
pthread_mutex_lock(&mutex);
while(p[i] != '\0')
{
buf[i] = p[i];
usleep(200);
i++;
}
printf("th2 buf:%s\n", buf);
//解锁
pthread_mutex_unlock(&mutex);
}
int main()
{
//申请互斥锁
pthread_mutex_init(&mutex, NULL);
pthread_t thId1, thId2;
char strTh1[100] = "abcddefghk";
char strTh2[100] = "1234567890";
pthread_create(&thId1, NULL, threadFunc1, strTh1);
pthread_create(&thId2, NULL, threadFunc2, strTh2);
//pthread_detach(thId1);
//pthread_detach(thId2);
//while(1);
pthread_join(thId1, NULL);
pthread_join(thId2, NULL);
return 0;
}
注意避免死锁发生