小白初识linux下的C语言多线程

简介: 小白初识linux下的C语言多线程

欢迎

下面举例 :创建52个线程

一个教授,一个助教,50个学生

#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
#include <time.h>
/* prototype for thread routine */
void print_message_function ( void *ptr );
/* struct to hold data to be passed to a thread
   this shows how multiple data items can be passed to a thread */
typedef struct str_thdata
{
    int  thread_no;
    char message[100];
}thdata;
int main()
{
    pthread_t thread[52];  /* thread variables */
    thdata data[52];         /* structs to be passed to threads */
    /* initialize data to pass to thread 1 */
    data[0].thread_no = 1;
    strcpy(data[0].message, "TY doctor!");
    /* initialize data to pass to thread 2 */
    data[1].thread_no = 2;
    strcpy(data[1].message, "yang teaching assistant!");
    /* initialize data to pass to thread 3-52 */
    for(int i=2;i<52;i++){
    data[i].thread_no = i+1;
    strcpy(data[i].message,"student");
    }
    srand((int)time(NULL));
    /* create threads 1 and 2 */    
    pthread_create (&thread[0], NULL, (void *) &print_message_function, (void *) &data[0]);
    usleep(1);
    pthread_create (&thread[1], NULL, (void *) &print_message_function, (void *) &data[1]);
  usleep(1);
  for(int i=2;i<52;i++){
    pthread_create (&thread[i], NULL, (void *) &print_message_function, (void *) &data[i]);
    int m=rand()%200;
    usleep(m);
  }
    /* Main block now waits for all threads to terminate, before it exits
       If main block exits, both threads exit, even if the threads have not
       finished their work */ 
    for(int j=0;j<52;j++)
      pthread_join(thread[j], NULL);
    /* exit */  
    exit(0);
} /* main() */
/**
 * print_message_function is used as the start routine for the threads used
 * it accepts a void pointer 
**/
void print_message_function ( void *ptr )
{
    thdata *data;            
    data = (thdata *) ptr;  /* type cast to a pointer to thdata */
    /* do the work */
    if(data->thread_no>2){
      printf("Thread %2d says %s %2d\n", data->thread_no, data->message,(data->thread_no-2));
    }
    else{
        printf("Thread %2d says %s \n", data->thread_no, data->message);
    }
    pthread_exit(0); /* exit */
} /* print_message_function ( void *ptr ) */


输出样例

$ gcc -o main main.c -lpthread
$ ./main
Thread  1 says doctor! 
Thread  2 says teaching assistant! 
Thread  3 says student  1
Thread  4 says student  2
Thread  5 says student  3
......
Thread 51 says student 49
Thread 52 says student 50


相关文章
|
27天前
|
安全 Java C语言
C语言线程解池解读和实现01
C语言线程解池解读和实现01
|
1月前
|
算法 Unix Linux
linux线程调度策略
linux线程调度策略
33 0
|
8天前
|
网络协议 C语言
C语言 网络编程(十四)并发的TCP服务端-以线程完成功能
这段代码实现了一个基于TCP协议的多线程服务器和客户端程序,服务器端通过为每个客户端创建独立的线程来处理并发请求,解决了粘包问题并支持不定长数据传输。服务器监听在IP地址`172.17.140.183`的`8080`端口上,接收客户端发来的数据,并将接收到的消息添加“-回传”后返回给客户端。客户端则可以循环输入并发送数据,同时接收服务器回传的信息。当输入“exit”时,客户端会结束与服务器的通信并关闭连接。
|
8天前
|
存储 Ubuntu Linux
C语言 多线程编程(1) 初识线程和条件变量
本文档详细介绍了多线程的概念、相关命令及线程的操作方法。首先解释了线程的定义及其与进程的关系,接着对比了线程与进程的区别。随后介绍了如何在 Linux 系统中使用 `pidstat`、`top` 和 `ps` 命令查看线程信息。文档还探讨了多进程和多线程模式各自的优缺点及适用场景,并详细讲解了如何使用 POSIX 线程库创建、退出、等待和取消线程。此外,还介绍了线程分离的概念和方法,并提供了多个示例代码帮助理解。最后,深入探讨了线程间的通讯机制、互斥锁和条件变量的使用,通过具体示例展示了如何实现生产者与消费者的同步模型。
|
8天前
|
C语言
C语言 网络编程(九)并发的UDP服务端 以线程完成功能
这是一个基于UDP协议的客户端和服务端程序,其中服务端采用多线程并发处理客户端请求。客户端通过UDP向服务端发送登录请求,并根据登录结果与服务端的新子线程进行后续交互。服务端在主线程中接收客户端请求并创建新线程处理登录验证及后续通信,子线程创建新的套接字并与客户端进行数据交换。该程序展示了如何利用线程和UDP实现简单的并发服务器架构。
|
8天前
|
Shell Linux API
C语言在linux环境下执行终端命令
本文介绍了在Linux环境下使用C语言执行终端命令的方法。首先,文章描述了`system()`函数,其可以直接执行shell命令并返回结果。接着介绍了更强大的`popen()`函数,它允许程序与命令行命令交互,并详细说明了如何使用此函数及其配套的`pclose()`函数。此外,还讲解了`fork()`和`exec`系列函数,前者创建新进程,后者替换当前进程执行文件。最后,对比了`system()`与`exec`系列函数的区别,并针对不同场景推荐了合适的函数选择。
|
23天前
|
C语言
【C语言】线程同步
【C语言】线程同步
27 3
|
24天前
|
存储 设计模式 NoSQL
Linux线程详解
Linux线程详解
|
1月前
|
缓存 Linux C语言
Linux线程是如何创建的
【8月更文挑战第5天】线程不是一个完全由内核实现的机制,它是由内核态和用户态合作完成的。
|
22天前
|
负载均衡 Linux 调度
在Linux中,进程和线程有何作用?
在Linux中,进程和线程有何作用?