Linux进程操作

一、主要内容
1、进程和程序:程序是静态的,它是一些保存在磁盘上的指令的有序集合,没有任何执行的概念。进程是一个动态的概念,它是程序的执行过程,包括创建、调度和消亡。
这里写图片描述
注意:进程不仅包括程序的指令和数据,还包括程序计数器、CPU的所有寄存器值以及存储临时数据的进程堆栈。

2、在统一进程中,多个线程共享以下资源:可执行的指令、静态数据、进程中打开的文件描述符、信号处理函数、当前工作目录、用户ID、用户组ID。

3、线程的私有资源:线程ID(TID)、PC(程序计数器)和相关寄存器、堆栈(局部变量和返回地址)、错误号(errno)、信号掩码和优先级、执行状态和属性。

4、多线程编程中创建一个线程用pthread_create(),Linux中对它的定义如下:
所需头文件 : #include

#include<stdio.h>
#include <pthread.h>

void *ThreadFunc(void *arg)
{
    printf("hello world\r\n");
}

int main()
{
    pthread_t tID = 0;
    if(0 != pthread_create(&tID,NULL,ThreadFunc,NULL))
    {
        printf("create error\r\n");
        return -1;
    }
    sleep(1);
    return 0;
}

2、pthread_arg.c

#include<stdio.h>
#include <pthread.h>

void *ThreadFunc(void *arg)
{
    if(NULL == arg)
    {
        return  (void *)NULL;
    }
    //printf("hello world\r\n");
    int b = *(int *)arg;
    printf("b=%d\r\n",b);
}

int main()
{
    pthread_t tID = 0;
    int a = 10;
    if(0 != pthread_create(&tID,NULL,ThreadFunc,(void *)&a))
    {
        printf("create error\r\n");
        return -1;
    }
//  sleep(1);
    pthread_join(tID,NULL);
    return 0;
}

3、pthread_join.c

#include<stdio.h>
#include <pthread.h>

void *ThreadFunc(void *arg)
{
    printf("hello world\r\n");
    pthread_exit("thread exit");
}

int main()
{
    pthread_t tID = 0;
    if(0 != pthread_create(&tID,NULL,ThreadFunc,NULL))
    {
        printf("create error\r\n");
        return -1;
    }
//  sleep(1);
    char *str = NULL;
    pthread_join(tID,(void *)&str);
    printf("%s\r\n",str);
    return 0;
}

4、pthread_mutex.c用互斥锁实现进程间的同步与互斥,起到保护现场的作用。保证第一次函数调用执行完才能执行第二次函数调用。

#include<stdio.h>
#include <pthread.h>

#define BUF_SIZE 11

pthread_mutex_t mutex;
void *Func(void *arg)
    if(NULL == arg)
    {
        return  (void *)NULL;
    }

    char *p = (char *)arg;
    pthread_mutex_lock(&mutex);
    static char buf[BUF_SIZE]={0};
    int i = 0;
    for(;i<10;i++)
    {
        buf[i]=p[i];
        sleep(1);
    }
    printf("buf=%s\r\n",buf);
    pthread_mutex_unlock(&mutex);
}

int main()static char buf[BUF_SIZE]={0};
    int i = 0;
    for(;i<10;i++)
    {
        buf[i]=p[i];
        sleep(1);
    }
    printf("buf=%s\r\n",buf);
{
    pthread_t tID1 = 0;
    pthread_t tID2 = 0;

    char arr1[]={'a','b','c','d','e','f','g','h','i','j'};
    char arr2[]={'1','2',3,'4','5','6','7','8','9','0'};
    if(-1 == pthread_mutex_init(&mutex,NULL))
    {
        return -1;
    }

    if(0 != pthread_create(&tID1,NULL,Func,arr1))
    {
        return -1;
    }

    if(0 != pthread_create(&tID2,NULL,Func,arr2))
    {
        return -1;
    }

    pthread_join(tID1,NULL);
    pthread_join(tID2,NULL);
    return 0;
}

5、pthread_sem.c函数实现进程间的互斥操作

#include<stdio.h>
#include <pthread.h>
#include <semaphore.h>

sem_t sem1;
sem_t sem2;

void *ThreadFunc1(void *arg)
{
    int i=10;
    while(i--)
    {
        sem_wait(&sem1);
        printf("hello\r\n");
        usleep(5000);
        sem_post(&sem2);
    }
}

void *ThreadFunc2(void *arg)
{
    int i=10;
    while(i--)
    {
        sem_wait(&sem2);
        printf("world\r\n");
        usleep(5000);
        sem_post(&sem1);
    }
}

int main()
{
    pthread_t tID1 = 0;
    pthread_t tID2 = 0;

    if(-1 == sem_init(&sem1,0,1) || -1 == sem_init(&sem2,0,0))
    {
        return -1;
    }

    if(0 != pthread_create(&tID1,NULL,ThreadFunc1,NULL))
    {
        printf("create error\r\n");
        return -1;
    }
    if(0 != pthread_create(&tID2,NULL,ThreadFunc2,NULL))
    {
        printf("create error\r\n");
        return -1;
    }

    pthread_join(tID1,NULL);
    pthread_join(tID2,NULL);

    return 0;
}

6、shm_r.c

#include<stdio.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <string.h>

#define SHM_SIZE 10

int main()
{
    //get key
    key_t key =ftok(".",11);
    if(-1 == key)
    {
        return -1;
    }
    printf("key ok\r\n");
    //shmget
    int shmid = shmget(key,SHM_SIZE,IPC_CREAT | 0666);
    if(-1==shmid)
    {
       return -1;
    }
    printf("shmget ok\r\n");
    //shm attach
    void *p =shmat(shmid,NULL,0);
    if((void*)-1==p)
    {
      return -1;
    }
    char *pTmp = (char *)p;
    int i = 0;
    char c = 0;
    for(;i<SHM_SIZE;i++)
    {
        c = pTmp[i];
        printf("%d ",c);
    }
    printf("\r\n");
    shmdt(p);
    //delete shm
    shmctl(shmid,IPC_RMID,NULL);
    return 0;
}

7、shm_w.c

#include<stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>

#define SHM_SIZE 10

int main()
{
    //get key
    key_t key = ftok(".",11);
    if(-1 == key)
    {
        return -1;
    }
    //shmget
    int shmid=shmget(key,SHM_SIZE,IPC_CREAT | 0666);
    if(-1==shmid)
    {
      return -1;
    }
    printf("create success\r\n");
    //shm attch
    void *p=shmat(shmid,NULL,0);
    if((void*)-1==p)
    {
      return -1;
    }
    memset(p,13,SHM_SIZE);
    shmdt(p);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值