c: thread in Ubuntu
/**
* @file helloworld.c
* @author your name (geovindu)
* @brief
* ide: vscode c11,c17
* @version 0.1
* @date 2023-10-21
*
* @copyright Copyright (c) 2023 站在巨人的肩膀上 Standing on the Shoulders of Giants
*
*/
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<malloc.h>
#include<time.h>
#include <unistd.h>
#include <sys/wait.h>
#include<threads.h>
#include<math.h>
#include "include/CheckTieck.h"
#include "include/TakeNumber.h"
//
#define threadcout 5
//
thrd_t threadId[threadcout];
//
size_t task=0;
mtx_t task_mtx;
/**
* @brief
*
*/
struct timespec duration={.tv_sec=1,.tv_nsec=0};
/**
* @brief 线程
*
* @param agr
* @return int
*/
int execrteTask(void *agr)
{
mtx_lock(&task_mtx);
size_t local_task = ++task;
mtx_unlock(&task_mtx);
// mtx_lock(&task_mtx); // mutex lock - blocks until acquired
// printf_s("Task %zd started.\n", ++task);
printf("Task %zd started.\n", local_task);
thrd_sleep(&duration, NULL); // Just to make things take longer...
double x = 0;
for(int i = 0 ; i< 1000000000 ; ++i)
x = sqrt(3.1415926);
printf(" Task %zd finished\n", local_task);
// printf_s(" Task %zd finished\n", task);
// mtx_unlock(&task_mtx); // mutex unlock - for use by other threads
return 0;
}
/**
* @brief
*
* @return int
*/
int main(void)
{
if(thrd_error == mtx_init(&task_mtx, mtx_timed))
{
fprintf(stderr, "Mutex creation failed.\n");
thrd_exit(-2);
}
// Create the threads to carry out the tasks concurrently
for(size_t i = 0 ; i<threadcout ; ++i)
if(thrd_error == thrd_create(&(threadId[i]), execrteTask, NULL))
{
fprintf(stderr, "Thread creation failed.\n");
thrd_exit(-1);
}
// Join the additional threads to the main thread
for(size_t j = 0 ; j <threadcout ; ++j)
thrd_join(threadId[j], NULL);
pid_t pid;
int status;
pid = fork(); // 创建一个新进程
if (pid < 0) { // 如果创建失败,输出错误信息
fprintf(stderr, "Fork Failed");
return 1;
} else if (pid == 0) { // 子进程
printf("I am the child %d\n",pid);
execl("/bin/ls", "ls", NULL); // 在子进程中执行 /bin/ls 程序
printf("I am the child %d, and execl failed\n",pid); // 如果 execl 返回,那么说明出错
} else { // 父进程
wait(&status); // 等待子进程结束
printf("I am the parent %d, and my child has ended\n",pid);
}
printf("hello wolrd, c launguage! weblcome geovindu!涂聚文");
QueueCalling *queue1;
char select='1';
//int num=1;//顾客序号
int num=0; //叫号编号
queue1=QueueInit(); //初始化队列
if(queue1==NULL)
{
printf("创建队列时出错!\n");
//getch();
getchar();
return 0;
}
do{
//这里处理,列表不会显示两次
if(select=='1' || select=='2')
{
printf("\n请选择具体操作:\n");
printf("1.新到顾客\n");
printf("2.下一个顾客\n");
printf("0.退出\n") ;
fflush(stdin);
}
select=getchar();//getch();
switch(select)
{
case '1':
add(queue1);
printf("\n现在共有%d位顾客在等候!\n",QueueLen(queue1));
break;
case '2':
next(queue1);
printf("\n现在共有%d位顾客在等候!\n",QueueLen(queue1));
break;
case '0':
break;
}
}while(select!='0');
QueueFree(queue1); //释放队列
//getch();
getchar();
return 0;
}

6.5. Thread Arguments and Return Values — Computer Systems Fundamentals (jmu.edu)
https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/ThreadArgs.html
/**
* @file helloworld.c
* @author your name (geovindu)
* @brief
* IDE vscode Ubuntu 20. c11,c17
* @version 0.1
* @date 2023-10-20
* https://www.devsurvival.com/multi-threaded-programming/
* @copyright Copyright (c) 2023 站在巨人的肩膀上 Standing on the Shoulders of Giants
*
*/
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<malloc.h>
#include<time.h>
#include<unistd.h>
#include<sys/wait.h>
#include<threads.h>
#include<math.h>
#include<pthread.h>
#include <semaphore.h>
#include "include/CheckTieck.h"
#include "include/TakeNumber.h"
//
#define threadcout 5
//
thrd_t threadId[threadcout];
//
size_t task=0;
mtx_t task_mtx;
/**
* @brief
*
*/
struct timespec duration={.tv_sec=1,.tv_nsec=0};
int sum = 0; // Global int to store the sum of the two arrays
sem_t mutex; // Synchronization Bit
/**
* @brief add number
*
* @param arg
* @return void*
*/
void *addNumber(void *arg){
int *ptr = (int *) arg;
while(*ptr != -1){
sem_wait(&mutex);
sum += *ptr;
printf("value: %d sum %d\n", *ptr,sum );
sem_post(&mutex);
ptr++;
}
return NULL;
}
/**
* @brief
*
*/
struct numbers {
int a;
int b;
int sum;
};
/**
* @brief
*
* @param _args
* @return void*
*/
void *sum_thread (void *_args)
{
/* Cast the arguments to the usable struct type */
struct numbers *args = (struct numbers *) _args;
/* Place the result into the struct itself (on the heap) */
args->sum = args->a + args->b;
printf("\nsum=%d\n",args->sum);
pthread_exit (NULL);
}
struct args {
int a;
int b;
};
/* struct for returning results from the child thread */
struct results {
int sum;
int difference;
int product;
int quotient;
int modulus;
};
/**
* @brief
*
* @param _args
* @return struct results*
*/
struct results *calculator (void *_args)
{
/* Cast the args to the usable struct type */
struct args *args = (struct args *) _args;
/* Allocate heap space for this thread's results */
struct results *results = calloc (sizeof (struct results), 1);
results->sum = args->a + args->b;
results->difference = args->a - args->b;
results->product = args->a * args->b;
results->quotient = args->a / args->b;
results->modulus = args->a % args->b;
printf("\nproduct:=%d\n",results->product);
/* De-allocate the input instance and return the pointer to
results on heap */
//free (args);
//pthread_exit (results); //退出线程
pthread_exit (NULL);
return results;
}
/**
* @brief 线程
*
* @param agr
* @return int
*/
int execrteTask(void *agr)
{
mtx_lock(&task_mtx);
size_t local_task = ++task;
mtx_unlock(&task_mtx);
// mtx_lock(&task_mtx); // mutex lock - blocks until acquired
// printf_s("Task %zd started.\n", ++task);
printf("Task %zd started.\n", local_task);
thrd_sleep(&duration, NULL); // Just to make things take longer...
double x = 0;
for(int i = 0 ; i< 1000000000 ; ++i)
x = sqrt(3.1415926);
printf(" Task %zd finished\n", local_task);
// printf_s(" Task %zd finished\n", task);
// mtx_unlock(&task_mtx); // mutex unlock - for use by other threads
return 0;
}
/**
* @brief
*
* @return int
*/
int main(void)
{
int A[4] = {1,5,3, -1}; // -1 marks the end of array
int B[4] = {4,9,6, -1};
pthread_t t_a, t_b;
sem_init(&mutex, 0, 1);
pthread_create(&t_a , NULL, addNumber, A);
pthread_create(&t_b, NULL, addNumber, B);
pthread_join(t_a, NULL);
pthread_join(t_b, NULL);
printf("Total: %d\n", sum);
int i;
pthread_t tid;
// Let us create three threads
for (i = 0; i < 3; i++)
{
pthread_create(&tid, NULL, calculator, (void *)&tid);
pthread_create(&tid, NULL, sum_thread, (void *)&tid);
}
//pthread_exit(NULL); //退出线程
if(thrd_error == mtx_init(&task_mtx, mtx_timed))
{
fprintf(stderr, "Mutex creation failed.\n");
thrd_exit(-2);
}
// Create the threads to carry out the tasks concurrently
for(size_t i = 0 ; i<threadcout ; ++i)
if(thrd_error == thrd_create(&(threadId[i]), execrteTask, NULL))
{
fprintf(stderr, "Thread creation failed.\n");
thrd_exit(-1);
}
// Join the additional threads to the main thread
for(size_t j = 0 ; j <threadcout ; ++j)
thrd_join(threadId[j], NULL);
printf("hello wolrd, c launguage! welcome geovindu!涂聚文");
QueueCalling *queue1;
char select='1';
//int num=1;//顾客序号
int num=0; //叫号编号
queue1=QueueInit(); //初始化队列
if(queue1==NULL)
{
printf("创建队列时出错!\n");
//getch();
getchar();
return 0;
}
do{
//这样处理,不会显示两次选择列表
if(select=='1' || select=='2')
{
printf("\n请选择具体操作:\n");
printf("1.新到顾客\n");
printf("2.下一个顾客\n");
printf("0.退出\n") ;
fflush(stdin);
}
select=getchar();//getch();
switch(select)
{
case '1':
add(queue1);
printf("\n现在共有%d位顾客在等候!\n",QueueLen(queue1));
break;
case '2':
next(queue1);
printf("\n现在共有%d位顾客在等候!\n",QueueLen(queue1));
break;
case '0':
break;
}
}while(select!='0');
QueueFree(queue1); //释放队列
//getch();
getchar();
return 0;
}

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号