linux下的C\\C++多进程多线程编程实例详解
作者:lqh 发布时间:2021-01-08 17:26:07
标签:linux,多进程,多线程
linux下的C\C++多进程多线程编程实例详解
1、多进程编程
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t child_pid;
/* 创建一个子进程 */
child_pid = fork();
if(child_pid == 0)
{
printf("child pid\n");
exit(0);
}
else
{
printf("father pid\n");
sleep(60);
}
return 0;
}
2、多线程编程
#include <stdio.h>
#include <pthread.h>
struct char_print_params
{
char character;
int count;
};
void *char_print(void *parameters)
{
struct char_print_params *p = (struct char_print_params *)parameters;
int i;
for(i = 0; i < p->count; i++)
{
fputc(p->character,stderr);
}
return NULL;
}
int main()
{
pthread_t thread1_id;
pthread_t thread2_id;
struct char_print_params thread1_args;
struct char_print_params thread2_args;
thread1_args.character = 'x';
thread1_args.count = 3000;
pthread_create(&thread1_id, NULL, &char_print, &thread1_args);
thread2_args.character = 'o';
thread2_args.count = 2000;
pthread_create(&thread2_id, NULL, &char_print, &thread2_args);
pthread_join(thread1_id, NULL);
pthread_join(thread2_id, NULL);
return 0;
}
3、线程同步与互斥
1)、互斥
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
/*也可以用下面的方式初始化*/
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex);
/* 互斥 */
thread_flag = value;
pthread_mutex_unlock(&mutex);
2)、条件变量
int thread_flag = 0;
pthread_mutex_t mutex;
pthread_cond_t thread_flag_cv;\
void init_flag()
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&thread_flag_cv, NULL);
thread_flag = 0;
}
void *thread_function(void *thread_flag)
{
while(1)
{
pthread_mutex_lock(&mutex);
while(thread_flag != 0 )
{
pthread_cond_wait(&thread_flag_cv, &mutex);
}
pthread_mutex_unlock(&mutex);
do_work();
}
return NULL;
}
void set_thread_flag(int flag_value)
{
pthread_mutex_lock(&mutex);
thread_flag = flag_value;
pthread_cond_signal(&thread_flag_cv);
pthread_mutex_unlock(&mutex);
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/nyist327/article/details/44958585


猜你喜欢
- 说明:在一台服务器上搭建redis一主二从三哨兵1.拉取镜像docker pull redis:42. 编写主 从配置文件2.1 创建/ho
- 引言:Exchange Server 2007提供了一个新的邮件兼容工具,称作传输规则机制。这种机制允许管理员对来自内外部的邮件信息采用策略
- 250定律 拉德认为:每一位顾客身后,大体有250名亲朋好友。如果您赢得了一位顾客的好感,就意味着赢得了250个人的好感;反之,如果你得罪了
- 现在很多中小网站(尤其是 Web 2.0 站点) 都允许用户上传图片,如果前期没有很好的规划,那么随着图片文件的增多,无论是管理还是性能上都
- 在"授权模式"对话框,选择客户端的授权模式,可以是"每客户"或"每服务器"方式。
- 实践证明,基于Windows NT 的Exchange Server并不是企业级电子邮件系统的最佳选择。由于对稳定性等性能要求非常高,因此大
- 今天,安装了CentOS系统,自带的YUM源更新太慢,所以更新使用了163的YUM源,具体更换方法:1.下载repo文件:wget http
- 首先在Idea中选择编辑运行配置,如下图左上角的“+”号,选择Tomcat服务,如下图自定义服务名称
- Ubuntu本身的源使用的是国内的源,下载速度比较慢,不像CentOS一样yum安装的时候对镜像站点进项选择, 所以选择了更换成国内的源。
- 一、概述最近在研究skywalking,打算使用k8s部署 skywalking 并将 pod 中的应用接入 skywalking 进行服务
- PHPWind v7.3.2开启门户模式为默认后打开read页面导致浏览器死掉问题。打开:mode/area/template/read.h
- 1、NFS简介(1)什么是NFS?1)NFS是Net File System的简写,即网络文件系统。NFS是由SUN公司开发,并于1984年
- 今天来介绍Godaddy主机用户如何在共享托管帐户上创建新增FTP用户。那Godaddy主机用户为什么需要在共享托管帐户上新增FTP用户?有
- 阳光总在风雨后,成功好象永远是在失败中找到。这是四月的第一场雨。清澈的雨把株洲的天空洗的非常明媚,然后清爽的阳光大把大把地散落人间。然而这样
- 网站的推广要随着互联网格局的变化而变化,不能死守着老一套,无论是传统的网站优化、友情链接、论坛博客等推广方式,还是新兴的SNS、游戏、回帖置
- linux系统下,我们可以修改或者添加.htaccess 文件来实现discuz的伪静态。1. 文章中引用的部分,则是实现伪静态的方法!我们
- 引言Rsync 是一种快速且通用的命令行实用程序,可通过远程shell在两个位置之间同步文件和文件夹。使用 Rsync,可以镜像数据,创建增
- 各位站长朋友,我在这里给大家拜年了!今天在这里谈论一下网站被黑的情况!首先,我不是一名黑客,但是网络安全知识还是稍了解一点。一般说自己是黑客
- 11月26日消息,针对“央视连推两大视频网站”一事,国内视频网站激动网认为,主流媒体进入视频行业将对整个行业发展起到非常积极的作用。激动网号