⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 process_shared_mutex.c

📁 linux 多线程例子
💻 C
字号:
/******************************************************** * An example source module to accompany... * * "Using POSIX Threads: Programming with Pthreads" *     by Brad nichols, Dick Buttlar, Jackie Farrell *     O'Reilly & Associates, Inc. * ******************************************************** * process_shared_mutex.c --  * * Demonstrating the shared process mutex */#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/wait.h>#include <pthread.h>#ifndef _POSIX_THREAD_PROCESS_SHARED#error "This system does not support process shared mutex"#endifint   shared_mem_id;int   *shared_mem_ptr;pthread_mutexattr_t mutex_shared_attr;pthread_mutex_t	*mptr;extern intmain(void){  pid_t  child_pid;  int  status, rtn;  /* initialize shared memory segment */  if ((shared_mem_id = shmget(IPC_PRIVATE, 1*sizeof(pthread_mutex_t), 0660)) < 0)    perror("shmget"), exit(1) ;  if ((shared_mem_ptr = (int *)shmat(shared_mem_id, (void *)0, 0)) == NULL)    perror("shmat"), exit(1);  mptr = (pthread_mutex_t *)shared_mem_ptr;  if (( rtn = pthread_mutexattr_init(&mutex_shared_attr)) != 0)     fprintf(stderr,"pthreas_mutexattr_init: %s",strerror(rtn)),exit(1);  if (( rtn = pthread_mutexattr_setshared(&mutex_shared_attr)) != 0)    fprintf(stderr,"pthread_mutexattr_setshared %s",strerror(rtn)),exit(1);  if (( rtn = pthread_mutex_init(mptr, &mutex_shared_attr)) != 0)    fprintf(stderr,"pthread_mutex_init %s",strerror(rtn)), exit(1);  if ((child_pid = fork()) == 0) {    /* first child */    if ((rtn = pthread_mutex_lock(mptr)) != 0)      fprintf(stderr,"child:pthread_mutex_lock %s",strerror(rtn)),exit(1);    sleep(1);    if ((rtn = pthread_mutex_unlock(mptr)) != 0)      fprintf(stderr,"child:pthread_unmutex_lock %s",strerror(rtn)),exit(1);    exit(0);  } else {    /* parent */     sleep(1);     if ((rtn = pthread_mutex_lock(mptr)) != 0)      fprintf(stderr,"parent:pthread_mutex_lock %d",strerror(rtn)),exit(1);    if ((rtn = pthread_mutex_unlock(mptr)) != 0)      fprintf(stderr,"child:pthread_unmutex_lock %d",strerror(rtn)),exit(1);    wait(&status);  }  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -