进程的概念:
具有一定独立功能的程序的一次运行活动,同时也是资源分配的最小单元。
运行了程序之后,创建的是一个进程,进程创建不同的执行流,诞生了线程。
线程是一个进程内部(虚拟空间)的控制序列(执行流)。
多线程:在单个进程中同时运行了多个线程。
多进程:是相对稳定的,多线程相对是不稳定的。
1、线程是进程的一个实体,线程是CPU调度和分配的基本单位,线程是程序执行的最小单位。
2、线程基本上是不拥有系统资源,只会用于一点在运行中必不可少的资源(线程ID,栈,信号。。。)
3、线程是可以与同属一个进程的其他线程共享进程拥有的资源。
4、进程是有独立空间的,当其中一个进程崩溃后,对其他进程是没有影响的,线程没有独立地址,当进程
中的一个线程死掉后,等同于整个进程死掉。
线程之间的切换效率比较高,进程之间的切换是比较费力地,费资源的;所以使用多线程可以提高运行效率和资源的利用率。
线程不能独立运行,只能存在于进程中。
-lpthread
互斥锁:
1、pthread_create(线程ID,NULL,线程函数,函数中的参数);
void * (*start_rtn)(void *);
void * (start)(void);
他的返回值是一个指针(void *),参数是void *;
2、pthread_exit(void * rval_ptr) 终止线程
(void *)100
3、pthread_join(退出的线程id,线程退出的返回值的指针)
①等待
②回收资源
相当于进程中的wait
4、pthread_setcanceltype
设置线程是否可被其他线程调用,pthread_cancel函数取消或终止
PTHREAD_CANCEL_ASYNCHRONOUS;线程接收到取消操作后,立即取消线程
5、pthread_cancel
发送终止信号给线程
6、线程分离
pthread_detach
①创建线程 pthread_create
②阻塞 pthread_join
③编写线程函数 Mythread1,2,3 pthread_setcanceltype(.....,old)
④终止线程 pthread_cancel(tid1,2,3)
msg_send.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#define MSGKEY 1234
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[64]; /* message data */
};
pthread_t tid1, tid2;
void *SendHandler(void *arg)
{
int ret;
struct msgbuf mbuf;
int msgid = *(int *)arg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while (1)
{
memset(&mbuf, 0, sizeof(mbuf));
mbuf.mtype = 1; //消息类型
scanf("%s", mbuf.mtext);
ret = msgsnd(msgid, &mbuf, sizeof(mbuf.mtext), 0);
if (-1 == ret)
{
perror("msgsnd");
exit(1);
}
if (!strcmp(mbuf.mtext, "bye"))
{
pthread_cancel(tid2);
break;
}
}
}
void *RecvHandler(void *arg)
{
int ret;
struct msgbuf mbuf;
int msgid = *(int *)arg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while (1)
{
memset(&mbuf, 0, sizeof(mbuf));
ret = msgrcv(msgid, &mbuf, sizeof(mbuf.mtext), 2, 0);
if (-1 == ret)
{
perror("msgrcv");
exit(1);
}
if (!strcmp(mbuf.mtext, "bye"))
{
pthread_cancel(tid1);
break;
}
printf("\t\t\t%s\n", mbuf.mtext);
memset(&mbuf, 0, sizeof(mbuf));
}
}
int main()
{
int ret;
int msgid = msgget(MSGKEY, IPC_CREAT | IPC_EXCL);
if (-1 == msgid)
{
perror("msgget");
exit(1);
}
//发送线程
ret = pthread_create(&tid1, NULL, SendHandler, &msgid);
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
//接收线程
ret = pthread_create(&tid2, NULL, RecvHandler, &msgid);
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status;
pthread_join(tid1, &status);
pthread_join(tid2, &status);
sleep(1);
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
msg_recv.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#define MSGKEY 1234
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[64]; /* message data */
};
pthread_t tid1, tid2;
void *SendHandler(void *arg)
{
struct msgbuf mbuf;
int ret;
int msgid = *(int *)arg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while (1)
{
memset(&mbuf, 0, sizeof(mbuf));
mbuf.mtype = 2; //消息类型
scanf("%s", mbuf.mtext);
ret = msgsnd(msgid, &mbuf, sizeof(mbuf.mtext), 0);
if (-1 == ret)
{
perror("msgsnd");
exit(1);
}
if (!strcmp(mbuf.mtext, "bye"))
{
pthread_cancel(tid2);
break;
}
}
}
void *RecvHandler(void *arg)
{
struct msgbuf mbuf;
int ret;
int msgid = *(int *)arg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while (1)
{
memset(&mbuf, 0, sizeof(mbuf));
ret = msgrcv(msgid, &mbuf, sizeof(mbuf.mtext), 1, 0);
if (-1 == ret)
{
perror("msgrcv");
exit(1);
}
if (!strcmp(mbuf.mtext, "bye"))
{
pthread_cancel(tid1);
break;
}
printf("\t\t\t%s\n", mbuf.mtext);
memset(&mbuf, 0, sizeof(mbuf));
}
}
int main()
{
int ret;
int msgid = msgget(MSGKEY, 0);
if (-1 == msgid)
{
perror("msgget");
exit(1);
}
//发送线程
ret = pthread_create(&tid1, NULL, SendHandler, &msgid);
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
//接收线程
ret = pthread_create(&tid2, NULL, RecvHandler, &msgid);
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status;
pthread_join(tid1, &status);
pthread_join(tid2, &status);
return 0;
}