/** * @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)) { printf(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)) { printf(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; }
标签:mtx,task,thread,int,printf,Task,Ubuntu,include From: https://www.cnblogs.com/geovindu/p/17781013.html