① 請教一個Linux下c語言的進程間的信號問題
linux中的進程通信分為三個部分:低級通信,管道通信和進程間通信IPC(inter process communication)。linux的低級通信主要用來傳遞進程的控制信號——文件鎖和軟中斷信號機制。linux的進程間通信IPC有三個部分——①信號量,②共享內存和③消息隊列。以下是我編寫的linux進程通信的C語言實現代碼。操作系統為redhat9.0,編輯器為vi,編譯器採用gcc。下面所有實現代碼均已經通過測試,運行無誤。
一.低級通信--信號通信
signal.c
#include
#include
#include
/*捕捉到信號sig之後,執行預先預定的動作函數*/
void sig_alarm(int sig)
{
printf("---the signal received is %d. /n", sig);
signal(SIGINT, SIG_DFL); //SIGINT終端中斷信號,SIG_DFL:恢復默認行為,SIN_IGN:忽略信號
}
int main()
{
signal(SIGINT, sig_alarm);//捕捉終端中斷信號
while(1)
{
printf("waiting here!/n");
sleep(1);
}
return 0;
}
二.管道通信
pipe.c
#include
#define BUFFER_SIZE 30
int main()
{
int x;
int fd[2];
char buf[BUFFER_SIZE];
char s[BUFFER_SIZE];
pipe(fd);//創建管道
while((x=fork())==-1);//創建管道失敗時,進入循環
/*進入子進程,子進程向管道中寫入一個字元串*/
if(x==0)
{
sprintf(buf,"This is an example of pipe!/n");
write(fd[1],buf,BUFFER_SIZE);
exit(0);
}
/*進入父進程,父進程從管道的另一端讀出剛才寫入的字元串*/
else
{
wait(0);//等待子進程結束
read(fd[0],s,BUFFER_SIZE);//讀出字元串,並將其儲存在char s[]中
printf("%s",s);//列印字元串
}
return 0;
}
三.進程間通信——IPC
①信號量通信
sem.c
#include
#include
#include
#include types.h>
#include ipc.h>
#include sem.h>
/*聯合體變數*/
union semun
{
int val; //信號量初始值
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
/*函數聲明,信號量定義*/
static int set_semvalue(void); //設置信號量
static void del_semvalue(void);//刪除信號量
static int semaphore_p(void); //執行P操作
static int semaphore_v(void); //執行V操作
static int sem_id; //信號量標識符
int main(int argc, char *argv[])
{
int i;
int pause_time;
char op_char = 'O';
srand((unsigned int)getpid());
sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT);//創建一個信號量,IPC_CREAT表示創建一個新的信號量
/*如果有參數,設置信號量,修改字元*/
if (argc > 1)
{
if (!set_semvalue())
{
fprintf(stderr, "Failed to initialize semaphore/n");
exit(EXIT_FAILURE);
}
op_char = 'X';
sleep(5);
}
for(i = 0; i < 10; i++)
{
/*執行P操作*/
if (!semaphore_p())
exit(EXIT_FAILURE);
printf("%c", op_char);
fflush(stdout);
pause_time = rand() % 3;
sleep(pause_time);
printf("%c", op_char);
fflush(stdout);
/*執行V操作*/
if (!semaphore_v())
exit(EXIT_FAILURE);
pause_time = rand() % 2;
sleep(pause_time);
}
printf("/n%d - finished/n", getpid());
if (argc > 1)
{
sleep(10);
del_semvalue(); //刪除信號量
}
exit(EXIT_SUCCESS);
}
/*設置信號量*/
static int set_semvalue(void)
{
union semun sem_union;
sem_union.val = 1;
if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
return(0);
return(1);
}
/*刪除信號量*/
static void del_semvalue(void)
{
union semun sem_union;
if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
fprintf(stderr, "Failed to delete semaphore/n");
}
/*執行P操作*/
static int semaphore_p(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1; /* P() */
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_p failed/n");
return(0);
}
return(1);
}
/*執行V操作*/
static int semaphore_v(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1; /* V() */
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_v failed/n");
return(0);
}
return(1);
}
②消息隊列通信
send.c
#include
#include
#include
#include
#include
#include types.h>
#include ipc.h>
#include msg.h>
#define MAX_TEXT 512
/*用於消息收發的結構體--my_msg_type:消息類型,some_text:消息正文*/
struct my_msg_st
{
long int my_msg_type;
char some_text[MAX_TEXT];
};
int main()
{
int running = 1;//程序運行標識符
struct my_msg_st some_data;
int msgid;//消息隊列標識符
char buffer[BUFSIZ];
/*創建與接受者相同的消息隊列*/
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
if (msgid == -1)
{
fprintf(stderr, "msgget failed with error: %d/n", errno);
exit(EXIT_FAILURE);
}
/*向消息隊列中發送消息*/
while(running)
{
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
some_data.my_msg_type = 1;
strcpy(some_data.some_text, buffer);
if (msgsnd(msgid, (void *)&some_data, MAX_TEXT, 0) == -1)
{
fprintf(stderr, "msgsnd failed/n");
exit(EXIT_FAILURE);
}
if (strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
exit(EXIT_SUCCESS);
}
receive.c
#include
#include
#include
#include
#include
#include types.h>
#include ipc.h>
#include msg.h>
/*用於消息收發的結構體--my_msg_type:消息類型,some_text:消息正文*/
struct my_msg_st
{
long int my_msg_type;
char some_text[BUFSIZ];
};
int main()
{
int running = 1;//程序運行標識符
int msgid; //消息隊列標識符
struct my_msg_st some_data;
long int msg_to_receive = 0;//接收消息的類型--0表示msgid隊列上的第一個消息
/*創建消息隊列*/
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
if (msgid == -1)
{
fprintf(stderr, "msgget failed with error: %d/n", errno);
exit(EXIT_FAILURE);
}
/*接收消息*/
while(running)
{
if (msgrcv(msgid, (void *)&some_data, BUFSIZ,msg_to_receive, 0) == -1)
{
fprintf(stderr, "msgrcv failed with error: %d/n", errno);
exit(EXIT_FAILURE);
}
printf("You wrote: %s", some_data.some_text);
if (strncmp(some_data.some_text, "end", 3) == 0)
{
running = 0;
}
}
/*刪除消息隊列*/
if (msgctl(msgid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "msgctl(IPC_RMID) failed/n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
③共享內存通信
share.h
#define TEXT_SZ 2048 //申請共享內存大小
struct shared_use_st
{
int written_by_you; //written_by_you為1時表示有數據寫入,為0時表示數據已經被消費者提走
char some_text[TEXT_SZ];
};
procer.c
#include
#include
#include
#include
#include types.h>
#include ipc.h>
#include shm.h>
#include "share.h"
int main()
{
int running = 1; //程序運行標志位
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
int shmid; //共享內存標識符
/*創建共享內存*/
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed/n");
exit(EXIT_FAILURE);
}
/*將共享內存連接到一個進程的地址空間中*/
shared_memory = shmat(shmid, (void *)0, 0);//指向共享內存第一個位元組的指針
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed/n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X/n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
/*生產者寫入數據*/
while(running)
{
while(shared_stuff->written_by_you == 1)
{
sleep(1);
printf("waiting for client.../n");
}
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer, TEXT_SZ);
shared_stuff->written_by_you = 1;
if (strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
/*該函數用來將共享內存從當前進程中分離,僅使得當前進程不再能使用該共享內存*/
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed/n");
exit(EXIT_FAILURE);
}
printf("procer exit./n");
exit(EXIT_SUCCESS);
}
customer.c
#include
#include
#include
#include
#include types.h>
#include ipc.h>
#include shm.h>
#include "share.h"
int main()
{
int running = 1;//程序運行標志位
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
int shmid; //共享內存標識符
srand((unsigned int)getpid());
/*創建共享內存*/
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed/n");
exit(EXIT_FAILURE);
}
/*將共享內存連接到一個進程的地址空間中*/
shared_memory = shmat(shmid, (void *)0, 0);//指向共享內存第一個位元組的指針
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed/n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X/n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->written_by_you = 0;
/*消費者讀取數據*/
while(running)
{
if (shared_stuff->written_by_you)
{
printf("You wrote: %s", shared_stuff->some_text);
sleep( rand() % 4 );
shared_stuff->written_by_you = 0;
if (strncmp(shared_stuff->some_text, "end", 3) == 0)
{
running = 0;
}
}
}
/*該函數用來將共享內存從當前進程中分離,僅使得當前進程不再能使用該共享內存*/
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed/n");
exit(EXIT_FAILURE);
}
/*將共享內存刪除,所有進程均不能再訪問該共享內存*/
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed/n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
摘自:
② C語言 union semun { }什麼意思
這個也叫共用體,用法跟結構體有點相同,但數據存儲「空間」跟結構體不一樣,具體用法參照http://ke..com/link?url=Ayg3do4Cy4aEK0JMO74-TRZcEHZ62x-A2gmsjKi--a
③ 怎麼用c語言編程 實現創建原語、撤銷原語、阻塞原語和喚醒原語
下,應該差不多
一、如何建立線程
用到的頭文件
(a)pthread.h
(b)semaphore.h
(c) stdio.h
(d)string.h
定義線程標識
pthread_t
創建線程
pthread_create
對應了一個函數作為線程的程序段
注意的問題
要保證進程不結束(在創建線程後加死循環)
在線程中加入While(1)語句,也就是死循環,保證進程不結束。
二、控制線程並發的函數
sem_t:信號量的類型
sem_init:初始化信號量
sem_wait:相當於P操作
sem_post:相當於V操作
三、實現原形系統
父親、母親、兒子和女兒的題目:
桌上有一隻盤子,每次只能放入一隻水果。爸爸專放蘋果,媽媽專放橘子,一個兒子專等吃盤子中的橘子,一個女兒專等吃盤子中的蘋果。分別用P,V操作和管程實現
每個對應一個線程
pthread_t father; father進程
pthread_t mother; mother進程
pthread_t son; son進程
pthread_t daughter; daughter進程
盤子可以用一個變數表示
sem_t empty;
各線程不是只做一次,可以是無限或有限次循環
用While(1)控制各線程無限次循環
輸出每次是那個線程執行的信息
printf("%s\n",(char *)arg);通過參數arg輸出對應線程執行信息
編譯方法
gcc hex.c -lpthread
生成默認的可執行文件a.out
輸入./a.out命令運行
查看結果:程序連續運行顯示出
father input an apple.
daughter get an apple.
mother input an orange.
son get an orange.
mother input an orange.
son get an orange.
………………..
四、程序源代碼
#include <stdio.h>
#include<string.h>
#include <semaphore.h>
#include <pthread.h>
sem_t empty; //定義信號量
sem_t applefull;
sem_t orangefull;
void *procf(void *arg) //father線程
{
while(1){
sem_wait(&empty); //P操作
printf("%s\n",(char *)arg);
sem_post(&applefull); //V操作
sleep(7);
}
}
void *procm(void *arg) //mother線程
{
while(1){
sem_wait(&empty);
printf("%s\n",(char *)arg);
sem_post(&orangefull);
sleep(3);
}
}
void *procs(void *arg) //son線程
{
while(1){
sem_wait(&orangefull);
printf("%s\n",(char *)arg);
sem_post(&empty);
sleep(2);
}
}
void *procd(void *arg) //daughter線程
{
while(1){
sem_wait(&applefull);
printf("%s\n",(char *)arg);
sem_post(&empty);
sleep(5);
}
}
main()
{
pthread_t father; //定義線程
pthread_t mother;
pthread_t son;
pthread_t daughter;
sem_init(&empty, 0, 1); //信號量初始化
sem_init(&applefull, 0, 0);
sem_init(&orangefull, 0, 0);
pthread_create(&father,NULL,procf,"father input an apple."); //創建線程
pthread_create(&mother,NULL,procm,"mother input an orange.");
pthread_create(&daughter,NULL,procd,"daughter get an apple.");
pthread_create(&son,NULL,procs,"son get an orange.");
while(1){} //循環等待
}
另外,站長團上有產品團購,便宜有保證
④ C語言生產者消費者進程代碼問題
實現一個隊列CQueue CQueue提供兩個公有成員函數 addTail():往隊列尾部增加一個元素 removeHead():讀出並移除隊列的第一個元素 生產者:兩個線程通過調用CQueue::addTail()往隊列中增加元素 消費者:一個線程通過調用CQueue::removeHead()從隊列中讀取元素 #include <iostream> #include <list> #include <windows.h> #include <process.h> using namespace std; #define P(sem) WaitForSingleObject(sem,INFINITE) #define V(sem) ReleaseSemaphore(sem,1,NULL) class CQueue { public: void addTail();//往隊列尾部增加一個元素 void removeHead();//讀出並移除隊列的第一個元素 private: list<int> L; }; CQueue buffer;//全局的緩沖區 const int buf_size = 10;//緩沖區大小 static int GOODS_ID = 0;//商品序號 const int procers = 3;//生產者數量 const int consumers = 8;//消費者數量 void ProcerThread(void* param); void ConsumerThread(void* param); HANDLE empty,occupy,op_mutex; int main() { int i; int p_id[procers],c_id[consumers];
⑤ C 語言多線程怎麼讀文件高效
C語言---多個線程讀取文件,其代碼如下:
#include
#include
#include
#include
#include
#include
#define THREAD_NUM 25
typedef struct
{undefined
FILE *_fp;
int _nThreadId;//第幾個線程
sem_t *_semLock;
}IDD_THREAD_PARAM;
void *ThreadFunc(void *args)
{undefined
char sLine[100+1];
FILE *fpRead = ((IDD_THREAD_PARAM *)args)->_fp;
sem_t *semLock = ((IDD_THREAD_PARAM *)args)->_semLock;
int nId = ((IDD_THREAD_PARAM *)args)->_nThreadId;
sem_wait(semLock);
while(!feof(fpRead))
{undefined
memset(sLine,0,sizeof(sLine));
fgets(sLine,100,fpRead);
fprintf(stderr,"Thread ID-%d:%s",nId,sLine);
}
sem_post(semLock);
}
int main()
{undefined
pthread_t *pThreads;
sem_t semLock;
pThreads = (pthread_t *)malloc(THREAD_NUM*sizeof(pthread_t));
sem_init(&semLock,0,1);
FILE *fp = fopen("test.txt","r");
//開始線程循環
IDD_THREAD_PARAM param;
for(int i=0;i
{undefined
memset(param,0,sizeof(IDD_THREAD_PARAM));
param._fp = fp;
param._nThreadId = i;
param._semLock = &semLock;
pthread_create((pThreads+i),NULL,ThreadFunc,param);
}
for(int i=0;i
pthread_join(*(pThreads+i),NULL);
free(pThreads);
pThreads = NULL;
fclose(fp);
fp = NULL;
return 0;
}
⑥ c語言實例,linux線程同步的信號量方式 謝謝
這么高的懸賞,實例放後面。信號量(sem),如同進程一樣,線程也可以通過信號量來實現通信,雖然是輕量級的。信號量函數的名字都以"sem_"打頭。線程使用的基本信號量函數有四個。
信號量初始化。
intsem_init(sem_t*sem,intpshared,unsignedintvalue);
這是對由sem指定的信號量進行初始化,設置好它的共享選項(linux只支持為0,即表示它是當前進程的局部信號量),然後給它一個初始值VALUE。
等待信號量。給信號量減1,然後等待直到信號量的值大於0。
intsem_wait(sem_t*sem);
釋放信號量。信號量值加1。並通知其他等待線程。
intsem_post(sem_t*sem);
銷毀信號量。我們用完信號量後都它進行清理。歸還佔有的一切資源。
intsem_destroy(sem_t*sem);
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<errno.h>
#definereturn_if_fail(p)if((p)==0){printf("[%s]:funcerror!/n",__func__);return;}
typedefstruct_PrivInfo
{
sem_ts1;
sem_ts2;
time_tend_time;
}PrivInfo;
staticvoidinfo_init(PrivInfo*thiz);
staticvoidinfo_destroy(PrivInfo*thiz);
staticvoid*pthread_func_1(PrivInfo*thiz);
staticvoid*pthread_func_2(PrivInfo*thiz);
intmain(intargc,char**argv)
{
pthread_tpt_1=0;
pthread_tpt_2=0;
intret=0;
PrivInfo*thiz=NULL;
thiz=(PrivInfo*)malloc(sizeof(PrivInfo));
if(thiz==NULL)
{
printf("[%s]:Failedtomallocpriv./n");
return-1;
}
info_init(thiz);
ret=pthread_create(&pt_1,NULL,(void*)pthread_func_1,thiz);
if(ret!=0)
{
perror("pthread_1_create:");
}
ret=pthread_create(&pt_2,NULL,(void*)pthread_func_2,thiz);
if(ret!=0)
{
perror("pthread_2_create:");
}
pthread_join(pt_1,NULL);
pthread_join(pt_2,NULL);
info_destroy(thiz);
return0;
}
staticvoidinfo_init(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
thiz->end_time=time(NULL)+10;
sem_init(&thiz->s1,0,1);
sem_init(&thiz->s2,0,0);
return;
}
staticvoidinfo_destroy(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
sem_destroy(&thiz->s1);
sem_destroy(&thiz->s2);
free(thiz);
thiz=NULL;
return;
}
staticvoid*pthread_func_1(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s2);
printf("pthread1:pthread1getthelock./n");
sem_post(&thiz->s1);
printf("pthread1:pthread1unlock/n");
sleep(1);
}
return;
}
staticvoid*pthread_func_2(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s1);
printf("pthread2:pthread2gettheunlock./n");
sem_post(&thiz->s2);
printf("pthread2:pthread2unlock./n");
sleep(1);
}
return;
}
⑦ c語言指令有哪些啊
第一章:緒論?
內核版本號格式:x.y.zz-www/x為主版本號,y為次版本號,zz為次次版本號,www為發行號/次版本號改變說明內核有重大變革,其偶數為穩定版本,奇數為尚在開發中的版本
第二章:基礎?
文件種類:-:txt,二進制/d:目錄/l:鏈接文件(link)/b:區塊設備文件/c:字元設備文件/p:管道
目錄結構:bin:可執行/boot:開機引導/dev:設備文件/etc:系統配置文件/lib:庫文件/mnt:設備掛載點/var:系統日誌/
命令:rmdir:刪除空目錄/find [path] [expression]/touch命令還可以修改指定文件的最近一次訪問時間/tar -czvf usr.tar.gz path/tar –zxvf usr.tar.gz/tar –cjvf usr.tar.bz2 path/tar –jxvf usr.tar.bz2
gcc:預處理:-g/I在頭文件搜索路徑中添加目錄,L在庫文件搜索路徑中
gdb:設置斷點:b/查看斷點信息:info
Makefile:make –f other_makefile/<:第一個依賴文件的名稱/@:目標文件的完整名稱/^:所有不重復的依賴文件/+:所有依賴文件(可能重復)
第三章:文件IO
read:read(fd, temp, size); /讀fd中長度為size的值到temp/返回0表示file為NULL
write:write(fd, buf, buf_size); /寫長度為buf_size的buf內容到fd中
lseek:lseek(fd, offset, SEEK_SET); /從文件開頭向後增加offset個位移量
unlink:從文件系統中刪除一個名字
open1:int open(const char * pathname, int flags, mode_t mode);/flags為讀寫方式/mode為許可權設置/O_EXCL:測試文件是否存在/O_TRUNC:若存在同名文件則刪除之並新建
open2:注意O_NONBLOCK
mmap.1:void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offsize);
mmap.2:mmap(start_addr, flength, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
fcntl:上鎖/int fcntl(int fd, int cmd, struct flock * lock);/對誰;做什麼;設置所做內容
select:fd_max+1,回傳讀狀況,回傳寫狀況,回傳異常,select等待的時間/NULL為永遠等待/0為從不等待/凡需某狀況則用之,反則(fd_set *)NULL之
FD_*那幾個函數……
一般出錯則返回-1
第四章:文件與目錄
硬鏈接與符號鏈接?
chdir改變目錄
0:in/1:out/2:err
第五章:內存管理
可執行文件存儲時:代碼區、數據區和未初始化區
棧:by編譯器,向低址擴展,連續,效率高/堆:by程序員
/etc/syslog.conf,系統log記錄文件/優先順序為-20時最高
第六章:進程和信號
程序代碼、數據、變數、文件描述符和環境/init的pid為1
execl族:int execl(const char * path, const char * arg, ....);/path即可執行文件的路徑,一般為./最後一個參數以NULL結束
waitpid:waitpid(pid_t pid,int * status,int options);/option:一般用WNOHANG,沒有已經結束的子進程則馬上返回,不等待
kill:int kill(pid_t pid,int sig);/發送信號sig給pid
void (*signal(int signum, void(* handler)(int)))(int);/第一個參數被滿足時,執行handler/第一個參數常用:SIG_IGN:忽略信號/SIG_DFL:恢復默認信號
第七章:線程
sem_init(sem_t *sem, int pshared, unsigned int value)/pshared為0/value即初始值
第八章:管道
1:write/0:read
第九章:信號量、共享內存和消息隊列
臨界資源:操作系統中只允許一個進程訪問的資源/臨界區:訪問臨界資源的那段代碼
信號量:建立聯系(semget),然後初始化,PV操作,最後destroy
共享內存沒有提供同步機制
第十章:套接字
UDP:無連接協議,無主客端的區分/實時性
TCP:位元組流/數據可靠性/網路可靠性
數據報:SOCK_STREAM/SOCK_DGRAM
其它
管道一章的both_pipe即父子進程間的全雙工管道通訊
關繫到信號和互斥的伺服器-客戶端程序
線程一章的class的multi_thread文件夾下的thread8.c
int main(void)
{
int data_processed;
int file_pipes_1[2];
int file_pipes_2[2];
char buffer[BUFSIZ + 1];
const char some_data[] = "123";
const char ch2p[] = "this is the string from child to the parent!";
const char p2ch[] = "this is the string from parent to the child!";
pid_t fork_result;
memset(buffer,'\0',sizeof(buffer));
if(pipe(file_pipes_1) == 0){
if(pipe(file_pipes_2) == 0){
fork_result = fork();
switch(fork_result){
case -1:
perror("fork error");
exit(EXIT_FAILURE);
case 0://child
close(file_pipes_1[1]);
close(file_pipes_2[0]);
printf("in the child!\n");
read(file_pipes_1[0],buffer, BUFSIZ);
printf("in the child, read_result is \"%s\"\n",buffer);
write(file_pipes_2[1],ch2p, sizeof(ch2p));
printf("in the child, write_result is \"%s\"\n",ch2p);
exit(EXIT_SUCCESS);
default://parent
close(file_pipes_1[0]);
close(file_pipes_2[1]);
printf("in the parent!\n");
write(file_pipes_1[1], p2ch, sizeof(p2ch));
printf("in the parent, write_result is \"%s\"\n",p2ch);
read(file_pipes_2[0],buffer, BUFSIZ);
printf("in the parent, read_result is \"%s\"\n",buffer);
exit(EXIT_SUCCESS);
}
}
}
}
#ifndef DBG
#define DBG
#endif
#undef DBG
#ifdef DBG
#define PRINTF(fmt, args...) printf("file->%s line->%d: " \
fmt, __FILE__, __LINE__, ##args)
#else
#define PRINTF(fmt, args...) do{}while(0);
#endif
int main(void)
{
PRINTF("%s\n", "hello!");
fprintf(stdout, "hello hust!\n");
return 0;
}
#define N 5
#define MAX 5
int nput = 0;
char buf[MAX][50];
char *buffer = "";
char buf_r[100];
sem_t mutex,full,avail;
void *proctor(void *arg);
void *consumer(void *arg);
int i = 0;
int main(int argc, char **argv)
{
int cnt = -1;
int ret;
int nput = 0;
pthread_t id_proce[10];
pthread_t id_consume;
ret = sem_init(&mutex, 0, 1);
ret = sem_init(&avail, 0, N);
ret = sem_init(&full, 0, 0);
for(cnt = 0; cnt < 6; cnt ++ ){
//pthread_create(&id_proce[cnt], NULL, (void *)proctor, &cnt);
pthread_create(&id_proce[cnt], NULL, (void *)proctor, (void *)cnt);
}
pthread_create(&id_consume, NULL, (void *)consumer, NULL);
for(cnt = 0; cnt < 6; cnt ++){
pthread_join(id_proce[cnt], NULL);
}
pthread_join(id_consume,NULL);
sem_destroy(&mutex);
sem_destroy(&avail);
sem_destroy(&full);
exit(EXIT_SUCCESS);
}
void *proctor(void *arg)
{
while(1){
sem_wait(&avail);
sem_wait(&mutex);
if(nput >= MAX * 3){
sem_post(&avail);
//sem_post(&full);
sem_post(&mutex);
return NULL;
}
sscanf(buffer + nput, "%s", buf[nput % MAX]);
//printf("write[%d] \"%s\" to the buffer[%d]\n", (*(int*)arg), buf[nput % MAX],nput % MAX);
printf("write[%d] \"%s\" to the buffer[%d]\n", (int)arg, buf[nput % MAX],nput % MAX);
nput ++;
printf("nput = %d\n", nput);
sem_post(&mutex);
sem_post(&full);
}
return NULL;
}
void *consumer(void *arg)
{
int nolock = 0;
int ret, nread, i;
for(i = 0; i < MAX * 3; i++)
{
sem_wait(&full);
sem_wait(&mutex);
memset(buf_r, 0, sizeof(buf_r));
strncpy(buf_r, buf[i % MAX], sizeof(buf[i % MAX]));
printf("read \"%s\" from the buffer[%d]\n\n",buf_r, i % MAX);
sem_post(&mutex);
sem_post(&avail);
//sleep(1);
}
return NULL;
}
⑧ C語言 信號量的疑惑。。
一個是Posix實現,一個是System V實現
使用的環境不一樣
一般來講SV的適用於進程同步,POSIX適用於線程同步
System V進程同步 api:semget/semop/semctl
POSIX 線程同步 api:sem_init/sem_destroy
不過POSIX貌似還會分為有名和無名信號量上面說的是無名信號量。
具體的還要看使用的環境。
⑨ C語言如何在線程間實現同步和互斥
線程之間的同步和互斥解決的問題是線程對共同資源進行訪問。Posix有兩種方式:
信號量和互斥鎖;信號量適用同時可用的資源為多個的情況;互斥鎖適用於線程可用的資源只有一個的情況
1、互斥鎖:互斥鎖是用加鎖的方式來控制對公共資源的原子操作(一旦開始進行就不會被打斷的操作)
互斥鎖只有上鎖和解鎖兩種狀態。互斥鎖可以看作是特殊意義的全局變數,因為在同一時刻只有一個線程能夠對互斥鎖進行操作;只有上鎖的進程才可以對公共資源進行訪問,其他進程只能等到該進程解鎖才可以對公共資源進行操作。
互斥鎖操作函數:
pthread_mutex_init();//初始化
pthread_mutex_lock();//上鎖 參數:pthread_mutex_t *mutex
pthread_mutex_trylock();//判斷上鎖 參數:pthread_mutex_t *mutex
pthread_mutex_unlock();//解鎖 參數:pthread_mutex_t *mutex
pthread_mutex_release();//消除互斥鎖 參數:pthread_mutex_t *mutex
互斥鎖分為快速互斥鎖、遞歸互斥鎖、檢錯互斥鎖;在 init 的時候確定
int pthread_mutex_t(pthread_mutex_t *mutex, const pthread_mutex_t mutexattr);
第一個參數:進行操作的鎖
mutexattr:鎖的類型,默認快速互斥鎖(阻塞)123456789
2、信號量:信號量本質上是一個計數器,在操作系統做用於PV原子操作;
P操作使計數器-1;V操作使計數器+1.
在互斥操作中可以是使用一個信號量;在同步操作中需要使用多個信號量,並設置不同的初始值安排它們順序執行
sem_init(); // 初始化操作
sem_wait(); // P操作,計數器減一;阻塞 參數:sem_t *sem
sem_trywait(); // P操作,計數器減一;非阻塞 參數:sem_t *sem
sem_post(); // V操作,計數器加一 參數:sem_t *sem
sem_destroy(); // 銷毀信號量 參數:sem_t *sem
sem_init(sem_t *sem, int pshared, int value);
pshared用於指定多少個進程共享;value初始值
⑩ sem_t的介紹
C語言中,信號量的數據類型為結構sem_t,它本質上是一個長整型的數。