前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【线程同步】进程间同步

【线程同步】进程间同步

作者头像
mindtechnist
发布2024-08-08 17:15:17
990
发布2024-08-08 17:15:17
举报
文章被收录于专栏:机器和智能

1. 互斥量mutex

进程间也可以通过互斥锁来达到同步的目的。在pthread_mutex_init初始化之前需要修改属性为进程间共享。

1.1 互斥量属性对象的创建与销毁

  • 头文件及函数原型
代码语言:javascript
复制
#include <pthread.h>

int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
  • 函数描述
    • The pthread_mutexattr_destroy() function shall destroy a mutex attributes object; the object becomes, in effect, uninitialized.
    • The pthread_mutexattr_init() function shall initialize a mutex attributes object attr with the default value for all of the attributes defined by the implementation.
  • 函数参数
    • attr:互斥量属性对象
  • 函数返回值 Upon successful completion, pthread_mutexattr_destroy() and pthread_mutexattr_init() shall return zero; otherwise, an error number shall be returned to indicate the error.

1.2 属性的设置与获取

  • 头文件及函数原型
代码语言:javascript
复制
#include <pthread.h>

int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
  • 函数描述
    • The pthread_mutexattr_getpshared() function shall obtain the value of the process-shared attribute from the attributes object referenced by attr.
    • The pthread_mutexattr_setpshared() function shall set the process-shared attribute in an initialized attributes object referenced by attr.
  • 函数参数
    • attr
    • pshared
  • 函数返回值 Upon successful completion, pthread_mutexattr_setpshared() shall return zero; otherwise, an error number shall be returned to indicate the error. Upon successful completion, pthread_mutexattr_getpshared() shall return zero and store the value of the process-shared attribute of attr into the object referenced by the pshared parameter. Otherwise, an error number shall be returned to indicate the error.

2. 文件锁

借助fcntl()函数来实现锁机制,操作文件的进程没有获得锁时,可以打开,但无法执行read和write操作。文件锁具有读写锁的特点,写独占,读共享,写优先级高。

  • 头文件及函数原型
代码语言:javascript
复制
#include <unistd.h>
#include <fcntl.h>

int fcntl(int fd, int cmd, ... /* arg */ );
  • 函数描述 fcntl() performs one of the operations described below on the open file descriptor fd. The operation is determined by cmd. 获取、设置文件访问控制属性。
  • 函数参数
    • struct flock { ... short l_type; /* Type of lock: F_RDLCK, F_WRLCK, F_UNLCK 锁的类型 */ short l_whence; /* How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_END 偏移位置 */ off_t l_start; /* Starting offset for lock 起始偏移 */ off_t l_len; /* Number of bytes to lock 长度,0表示整个文件加锁 */ pid_t l_pid; /* PID of process blocking our lock (F_GETLK only) 持有该锁的进程ID */ ... };
    • fd:文件描述符
    • cmd:
      • F_SETLK (struct flock *):设置文件锁trylock
      • F_SETLKW (struct flock *):设置文件锁lock
      • F_GETLK (struct flock *):获取文件锁
  • 函数返回值
    • For a successful call, the return value depends on the operation:
      • F_DUPFD The new descriptor.
      • F_GETFD Value of flags.
      • F_GETFL Value of flags.
      • F_GETLEASE Type of lease held on file descriptor.
      • F_GETOWN Value of descriptor owner.
      • F_GETSIG Value of signal sent when read or write becomes possible, or zero for traditional SIGIO behavior. All other commands Zero.
    • On error, -1 is returned, and errno is set appropriately.

用法示例:

代码语言:javascript
复制
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define _FILE_PATH_ "/home/qq/dm/daemon/test.lock"

int main(int argc, char* argv[])
{
    int fd = open(_FILE_PATH_, O_RDWR | O_CREAT, 0644);
    if(fd < 0)
    {
        perror("open err");
        return -1;
    }
    struct flock lk;
    lk.l_type = F_WRLCK;
    lk.l_whence = SEEK_SET;
    lk.l_start = 0;
    lk.l_len = 0;
    if(fcntl(fd, F_SETLK, &lk) < 0)
    {
        perror("lock err");
        exit(1);
    }
    while(1)
    {
        printf("pid: %d\n", getpid());
        sleep(1);
    }
    return 0;
}

编译运行

此时我们再开一个终端运行上面的程序

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-04-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器和智能 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 互斥量mutex
    • 1.1 互斥量属性对象的创建与销毁
      • 1.2 属性的设置与获取
      • 2. 文件锁
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档