【Linux线程同步专题】五、进程间同步

简介: 【Linux线程同步专题】五、进程间同步

1. 互斥量mutex

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

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

  • 头文件及函数原型
#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 属性的设置与获取

  • 头文件及函数原型
#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操作。文件锁具有读写锁的特点,写独占,读共享,写优先级高。

  • 头文件及函数原型
#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. 获取、设置文件访问控制属性。
  • 函数参数
  • fd:文件描述符
  • cmd:
  • F_SETLK (struct flock *):设置文件锁trylock
  • F_SETLKW (struct flock *):设置文件锁lock
  • F_GETLK (struct flock *):获取文件锁
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 */
  ...
};
  • 函数返回值
  • 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.

用法示例:

#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;
}

编译运行

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

相关文章
|
安全 算法 Java
Java 多线程:线程安全与同步控制的深度解析
本文介绍了 Java 多线程开发的关键技术,涵盖线程的创建与启动、线程安全问题及其解决方案,包括 synchronized 关键字、原子类和线程间通信机制。通过示例代码讲解了多线程编程中的常见问题与优化方法,帮助开发者提升程序性能与稳定性。
672 0
|
Java 测试技术 API
【JUC】(1)带你重新认识进程与线程!!让你深层次了解线程运行的睡眠与打断!!
JUC是什么?你可以说它就是研究Java方面的并发过程。本篇是JUC专栏的第一章!带你了解并行与并发、线程与程序、线程的启动与休眠、打断和等待!全是干货!快快快!
1422 2
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
784 1
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
698 1
|
并行计算 Linux
Linux内核中的线程和进程实现详解
了解进程和线程如何工作,可以帮助我们更好地编写程序,充分利用多核CPU,实现并行计算,提高系统的响应速度和计算效能。记住,适当平衡进程和线程的使用,既要拥有独立空间的'兄弟',也需要在'家庭'中分享和并行的成员。对于这个世界,现在,你应该有一个全新的认识。
594 67
|
Unix Linux
对于Linux的进程概念以及进程状态的理解和解析
现在,我们已经了解了Linux进程的基础知识和进程状态的理解了。这就像我们理解了城市中行人的行走和行为模式!希望这个形象的例子能帮助我们更好地理解这个重要的概念,并在实际应用中发挥作用。
398 20
|
Web App开发 Linux 程序员
获取和理解Linux进程以及其PID的基础知识。
总的来说,理解Linux进程及其PID需要我们明白,进程就如同汽车,负责执行任务,而PID则是独特的车牌号,为我们提供了管理的便利。知道这个,我们就可以更好地理解和操作Linux系统,甚至通过对进程的有效管理,让系统运行得更加顺畅。
589 16
|
Ubuntu Linux
Linux系统管理:服务器时间与网络时间同步技巧。
以上就是在Linux服务器上设置时间同步的方式。然而,要正确运用这些知识,需要理解其背后的工作原理:服务器根据网络中的其他机器的时间进行校对,逐步地精确自己的系统时间,就像一只犹豫不决的啮齿动物,通过观察其他啮齿动物的行为,逐渐确定自己的行为逻辑,既简单,又有趣。最后希望这个过程既能给你带来乐趣,也能提高你作为系统管理员的专业素养。
2648 20
|
调度 开发工具 Android开发
【HarmonyOS Next】鸿蒙应用进程和线程详解
进程的定义: 进程是系统进行资源分配的基本单位,是操作系统结构的基础。 在鸿蒙系统中,一个应用下会有三类进程:
620 0
|
SQL 监控 网络协议
YashanDB进程线程体系
YashanDB进程线程体系