當前位置:首頁 » 操作系統 » linux的sem

linux的sem

發布時間: 2022-05-11 01:36:19

linux 下的 struct sembuf 是什麼數據結構阿

在sembuf結構中,sem_num是相對應的信號量集中的某一個資源,所以其值是一個從0到相應的信號量集的資源總數(ipc_perm.sem_nsems)之間的整數。sem_op指明所要執行的操作,sem_flg說明函數semop的行為。sem_op的值是一個整數.釋放相應的資源數,將sem_op的值加到信號量的值上.

㈡ linux 信號量問題 編譯錯誤 好像不識別sem_t定義的變數

幫你修改了一下,編譯運行沒問題,修改的地方都標出來了,
由於不知道你程序的功能,所以沒有對你的程序邏輯進行分析

#include <stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
//----------------以下是修改的部分
sem_t in;
sem_t out;
sem_t handout;
sem_t handin;
sem_t goout;
//----------------

int counter=0;

void * studentIn(void *a)
{
sem_wait(&in);//修改
counter++;
printf("%d\n",counter);
if(counter==30)
{
sem_post(&handout);//修改
return NULL;
}
sem_post(&in);//修改
return NULL;
}

void * fteacherhandout(void *b)
{
sem_wait(&handout);//修改
printf("teacher said:hand out over\n");
sem_post(&handin);//修改
return NULL;
}

void * studentout(void *c)
{
sem_wait(&handin);//修改
sem_wait(&out);//修改
counter--;
printf("%d\n",counter);
if(counter==0)
{
sem_post(&goout);//修改
return NULL;
}
sem_post(&out);//修改
}
void * fteacherout(void *d)
{
sem_wait(&goout);//修改
printf("teacher go out");
return NULL;
}

void main()
{

int i=0;
//----------------以下是修改的部分
sem_init(&in,0,1);
sem_init(&out,0,1);
sem_init(&handin,0,0);
sem_init(&handout,0,0);
sem_init(&goout,0,0);
//----------------
pthread_t thread1[30],thread2[30],teacher1,teacher2;
pthread_attr_t attr;
pthread_attr_init(&attr);
for(i=0;i<30;i++)
{
pthread_create(&thread1[i],&attr,studentIn,NULL);

}
for(i=0;i<30;i++)
{
pthread_create(&thread2[i],&attr,studentout,NULL);

}
pthread_create(&teacher1,&attr,fteacherhandout,NULL);

pthread_create(&teacher2,&attr,fteacherout,NULL);

return;
}

㈢ linux/sem.h和sys/sem.h有什麼區別

你好。

不只是sem.h,很多頭文件都有/usr/include/linux和/usr/include/sys兩個版本。

我的印象是:linux目錄裡面的頭文件用於內核,

sys目錄裡面的頭文件用於應用程序或者glibc標准庫

㈣ 怎麼修改linux中sem的值

semget() 可以使用系統調用semget()創建一個新的信號量集,或者存取一個已經存在的信號量集:
系統調用:semget();
原型:intsemget(key_t key,int nsems,int semflg);
返回值:如果成功,則返回信號量集的IPC標識符。如果失敗,則返回-1:errno=EACCESS(沒有許可權)
EEXIST(信號量集已經存在,無法創建)
EIDRM(信號量集已經刪除)
ENOENT(信號量集不存在,同時沒有使用IPC_CREAT)
ENOMEM(沒有足夠的內存創建新的信號量集)
ENOSPC(超出限制)
系統調用semget()的第一個參數是關鍵字值(一般是由系統調用ftok()返回的)。系統內核將此值和系統中存在的其他的信號量集的關鍵字值進行比 較。打開和存取操作與參數semflg中的內容相關。IPC_CREAT如果信號量集在系統內核中不存在,則創建信號量集。IPC_EXCL當和 IPC_CREAT一同使用時,如果信號量集已經存在,則調用失敗。如果單獨使用IPC_CREAT,則semget()要麼返回新創建的信號量集的標識 符,要麼返回系統中已經存在的同樣的關鍵字值的信號量的標識符。如果IPC_EXCL和IPC_CREAT一同使用,則要麼返回新創建的信號量集的標識 符,要麼返回-1。IPC_EXCL單獨使用沒有意義。參數nsems指出了一個新的信號量集中應該創建的信號量的個數。信號量集中最多的信號量的個數是 在linux/sem.h中定義的:

㈤ linux下sem多大值會溢出sem

一、你是想問:sem隊列的最大深度嗎?

int semget(key_t key, int nsems, int semflg);

nsems must be greater than 0 and less than or equal to the maximum number of semaphores per semaphore set (SEMMSL).

#define SEMMSL 250 /* <= 8 000 max num of semaphores per id */


semget的最大隊列深度為250

二、你想問的是sem id的最大值

/* Maximum value the semaphore can have. */

#define SEM_VALUE_MAX (2147483647)

㈥ Linux多進程和線程同步的幾種方式

Linux 線程同步的三種方法
線程的最大特點是資源的共享性,但資源共享中的同步問題是多線程編程的難點。linux下提供了多種方式來處理線程同步,最常用的是互斥鎖、條件變數和信號量。
一、互斥鎖(mutex)
通過鎖機制實現線程間的同步。
初始化鎖。在Linux下,線程的互斥量數據類型是pthread_mutex_t。在使用前,要對它進行初始化。
靜態分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
動態分配:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);
加鎖。對共享資源的訪問,要對互斥量進行加鎖,如果互斥量已經上了鎖,調用線程會阻塞,直到互斥量被解鎖。
int pthread_mutex_lock(pthread_mutex *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
解鎖。在完成了對共享資源的訪問後,要對互斥量進行解鎖。
int pthread_mutex_unlock(pthread_mutex_t *mutex);
銷毀鎖。鎖在是使用完成後,需要進行銷毀以釋放資源。
int pthread_mutex_destroy(pthread_mutex *mutex);
[csharp] view plain
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
#include "iostream"
using namespace std;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int tmp;
void* thread(void *arg)
{
cout << "thread id is " << pthread_self() << endl;
pthread_mutex_lock(&mutex);
tmp = 12;
cout << "Now a is " << tmp << endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
int main()
{
pthread_t id;
cout << "main thread id is " << pthread_self() << endl;
tmp = 3;
cout << "In main func tmp = " << tmp << endl;
if (!pthread_create(&id, NULL, thread, NULL))
{
cout << "Create thread success!" << endl;
}
else
{
cout << "Create thread failed!" << endl;
}
pthread_join(id, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
//編譯:g++ -o thread testthread.cpp -lpthread
二、條件變數(cond)
互斥鎖不同,條件變數是用來等待而不是用來上鎖的。條件變數用來自動阻塞一個線程,直到某特殊情況發生為止。通常條件變數和互斥鎖同時使用。條件變數分為兩部分: 條件和變數。條件本身是由互斥量保護的。線程在改變條件狀態前先要鎖住互斥量。條件變數使我們可以睡眠等待某種條件出現。條件變數是利用線程間共享的全局變數進行同步的一種機制,主要包括兩個動作:一個線程等待"條件變數的條件成立"而掛起;另一個線程使"條件成立"(給出條件成立信號)。條件的檢測是在互斥鎖的保護下進行的。如果一個條件為假,一個線程自動阻塞,並釋放等待狀態改變的互斥鎖。如果另一個線程改變了條件,它發信號給關聯的條件變數,喚醒一個或多個等待它的線程,重新獲得互斥鎖,重新評價條件。如果兩進程共享可讀寫的內存,條件變數可以被用來實現這兩進程間的線程同步。
初始化條件變數。
靜態態初始化,pthread_cond_t cond = PTHREAD_COND_INITIALIER;
動態初始化,int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
等待條件成立。釋放鎖,同時阻塞等待條件變數為真才行。timewait()設置等待時間,仍未signal,返回ETIMEOUT(加鎖保證只有一個線程wait)
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
激活條件變數。pthread_cond_signal,pthread_cond_broadcast(激活所有等待線程)
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond); //解除所有線程的阻塞
清除條件變數。無線程等待,否則返回EBUSY
int pthread_cond_destroy(pthread_cond_t *cond);
[cpp] view plain
#include <stdio.h>
#include <pthread.h>
#include "stdlib.h"
#include "unistd.h"
pthread_mutex_t mutex;
pthread_cond_t cond;
void hander(void *arg)
{
free(arg);
(void)pthread_mutex_unlock(&mutex);
}
void *thread1(void *arg)
{
pthread_cleanup_push(hander, &mutex);
while(1)
{
printf("thread1 is running\n");
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("thread1 applied the condition\n");
pthread_mutex_unlock(&mutex);
sleep(4);
}
pthread_cleanup_pop(0);
}
void *thread2(void *arg)
{
while(1)
{
printf("thread2 is running\n");
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("thread2 applied the condition\n");
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
pthread_t thid1,thid2;
printf("condition variable study!\n");
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&thid1, NULL, thread1, NULL);
pthread_create(&thid2, NULL, thread2, NULL);
sleep(1);
do
{
pthread_cond_signal(&cond);
}while(1);
sleep(20);
pthread_exit(0);
return 0;
}
[cpp] view plain
#include <pthread.h>
#include <unistd.h>
#include "stdio.h"
#include "stdlib.h"
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct node
{
int n_number;
struct node *n_next;
}*head = NULL;

static void cleanup_handler(void *arg)
{
printf("Cleanup handler of second thread./n");
free(arg);
(void)pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
struct node *p = NULL;
pthread_cleanup_push(cleanup_handler, p);
while (1)
{
//這個mutex主要是用來保證pthread_cond_wait的並發性
pthread_mutex_lock(&mtx);
while (head == NULL)
{
//這個while要特別說明一下,單個pthread_cond_wait功能很完善,為何
//這里要有一個while (head == NULL)呢?因為pthread_cond_wait里的線
//程可能會被意外喚醒,如果這個時候head != NULL,則不是我們想要的情況。
//這個時候,應該讓線程繼續進入pthread_cond_wait
// pthread_cond_wait會先解除之前的pthread_mutex_lock鎖定的mtx,
//然後阻塞在等待對列里休眠,直到再次被喚醒(大多數情況下是等待的條件成立
//而被喚醒,喚醒後,該進程會先鎖定先pthread_mutex_lock(&mtx);,再讀取資源
//用這個流程是比較清楚的
pthread_cond_wait(&cond, &mtx);
p = head;
head = head->n_next;
printf("Got %d from front of queue/n", p->n_number);
free(p);
}
pthread_mutex_unlock(&mtx); //臨界區數據操作完畢,釋放互斥鎖
}
pthread_cleanup_pop(0);
return 0;
}
int main(void)
{
pthread_t tid;
int i;
struct node *p;
//子線程會一直等待資源,類似生產者和消費者,但是這里的消費者可以是多個消費者,而
//不僅僅支持普通的單個消費者,這個模型雖然簡單,但是很強大
pthread_create(&tid, NULL, thread_func, NULL);
sleep(1);
for (i = 0; i < 10; i++)
{
p = (struct node*)malloc(sizeof(struct node));
p->n_number = i;
pthread_mutex_lock(&mtx); //需要操作head這個臨界資源,先加鎖,
p->n_next = head;
head = p;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx); //解鎖
sleep(1);
}
printf("thread 1 wanna end the line.So cancel thread 2./n");
//關於pthread_cancel,有一點額外的說明,它是從外部終止子線程,子線程會在最近的取消點,退出
//線程,而在我們的代碼里,最近的取消點肯定就是pthread_cond_wait()了。
pthread_cancel(tid);
pthread_join(tid, NULL);
printf("All done -- exiting/n");
return 0;
}
三、信號量(sem)
如同進程一樣,線程也可以通過信號量來實現通信,雖然是輕量級的。信號量函數的名字都以"sem_"打頭。線程使用的基本信號量函數有四個。
信號量初始化。
int sem_init (sem_t *sem , int pshared, unsigned int value);
這是對由sem指定的信號量進行初始化,設置好它的共享選項(linux 只支持為0,即表示它是當前進程的局部信號量),然後給它一個初始值VALUE。
等待信號量。給信號量減1,然後等待直到信號量的值大於0。
int sem_wait(sem_t *sem);
釋放信號量。信號量值加1。並通知其他等待線程。
int sem_post(sem_t *sem);
銷毀信號量。我們用完信號量後都它進行清理。歸還佔有的一切資源。
int sem_destroy(sem_t *sem);
[cpp] view plain
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__);return;}
typedef struct _PrivInfo
{
sem_t s1;
sem_t s2;
time_t end_time;
}PrivInfo;

static void info_init (PrivInfo* thiz);
static void info_destroy (PrivInfo* thiz);
static void* pthread_func_1 (PrivInfo* thiz);
static void* pthread_func_2 (PrivInfo* thiz);

int main (int argc, char** argv)
{
pthread_t pt_1 = 0;
pthread_t pt_2 = 0;
int ret = 0;
PrivInfo* thiz = NULL;
thiz = (PrivInfo* )malloc (sizeof (PrivInfo));
if (thiz == NULL)
{
printf ("[%s]: Failed to malloc priv./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);
return 0;
}
static void info_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;
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
sem_destroy (&thiz->s1);
sem_destroy (&thiz->s2);
free (thiz);
thiz = NULL;
return;
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail(thiz != NULL);
while (time(NULL) < thiz->end_time)
{
sem_wait (&thiz->s2);
printf ("pthread1: pthread1 get the lock./n");
sem_post (&thiz->s1);
printf ("pthread1: pthread1 unlock/n");
sleep (1);
}
return;
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
while (time (NULL) < thiz->end_time)
{
sem_wait (&thiz->s1);
printf ("pthread2: pthread2 get the unlock./n");
sem_post (&thiz->s2);
printf ("pthread2: pthread2 unlock./n");
sleep (1);
}
return;
}

㈦ linux 信號量是什麼怎麼用

Linux信號量(semaphore)是一種互斥機制。即對某個互斥資源的訪問會收到信號量的保護,在訪問之前需要獲得信號量。
在操作完共享資源後,需釋放信號量,以便另外的進程來獲得資源。獲得和釋放應該成對出現。
獲得信號量集,需要注意的是,獲得的是一個集合,而不是一個單一的信號量。
#include
#include
#include
1: int semget(key_t key,int nsems,int semflg);
key:系統根據這個值來獲取信號量集。
nsems:此信號集包括幾個信號量。
semflg:創建此信號量的屬性。 (IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)
成功則返回該信號量集的ID。
註:
既指定IPC_CREAT又指定IPC_EXCL時,如果系統中該信號量集已經存在,則馬上返回。
如果需要獲得存在的信號量,則將此參數置0.
2: int semctl(int semid,int senum,int cmd....)
semid:信號量ID。
senum:對信號量集中的第幾個信號量進行控制。(從0開始)
cmd:需要進行的操作。(SETVAL是其中的一個)。
根據cmd的不同可能存在第四個參數,cmd=SETVAL時,表示同時信號量可以被獲得幾次,如第四個參數
num=1表示只能被獲得一次,既被信號量保護的資源只能同時被一個程序使用。
該系統調用,是在對信號量初始化時用的。
-3: 「3」前面加了"-"表示當需要使用互斥資源時應該做這步。
int semop(int semid,struct sembuf *sem,int num_elements);
struct sembuf {
unsigned short sem_num; //該信號量集中的第幾個信號量。
int sem_op;//需要獲得還是釋放信號量
int sem_flg;//相關動作
};
num_elements:需要對該信號量集中的多少個信號量進行處理。
獲得信號量時,將sembuf結構提初始化為:
sem_num = 0; //該信號量集中的首個信號量
sem_op = -1; //獲得信號量
sem_flag = IPC_NOWAIT; //如果不能獲得信號量,馬上返回。
semop(semid,_sem,1);
同理釋放信號量時,將sem_op設為1.
以上是對信號量的簡單處理

㈧ linux編程時的信號量問題。 我以前用過的信號量頭文件是<semaphore.h>,而現在又發現還有個<sys/sem.h>

semaphore.h 提供的是 POSIX 標準定義的 semaphore 介面 ( sem_open, sem_wait, ...) ,這組介面使用更簡單,設計的較好。

而 sys/sem.h 里 提供的是符合 System V 標準的 semaphore介面 (semget, semop, ...),這些介面都比較老了, linux提供主要是為了兼容老代碼。

對於 linux 開發來說,新寫的代碼,都應該考慮採用 POSIX 標準的信號量。

㈨ linux 判斷有明信號量是否已經創建

sem_init:初始化信號量sem_t,初始化的時候可以指定信號量的初始值,以及是否可以在多進程間共享。 sem_wait:一直阻塞等待直到信號量>0。 sem_timedwait:阻塞等待若干時間直到信號量>0。 sem_post:使信號量加1。 sem_destroy:釋放信號量。...

㈩ 關於linux下的多線程使用sem信號量的運行問題

不是信號量的問題
printf函數,是先寫到輸出緩沖,遇到\n時,或者緩沖區滿時,或者有強制輸出(fflush)時,才會將緩沖區里的內容輸出到屏幕上(標准輸出設備:stdout)。你的代碼裡面並沒有以上3個觸發條件的任意一種,所以printf的內存沒有實際輸出到屏幕上。
你只要在每個printf函數後面加上fflush(stdout);就可以了。

熱點內容
安卓系統總是被殺後台怎麼辦 發布:2024-10-09 07:11:31 瀏覽:304
花雨庭伺服器如何全屏 發布:2024-10-09 06:39:28 瀏覽:213
密碼查看器怎麼使用 發布:2024-10-09 06:38:55 瀏覽:495
sqlrownum 發布:2024-10-09 06:28:53 瀏覽:383
F模塊驅動器編譯錯誤 發布:2024-10-09 06:06:21 瀏覽:636
腳本亞索集錦 發布:2024-10-09 05:53:30 瀏覽:877
安卓手機格式化後為什麼打不開 發布:2024-10-09 05:52:58 瀏覽:511
雲伺服器可以超級計算機嗎 發布:2024-10-09 05:51:33 瀏覽:17
php基本語法手冊 發布:2024-10-09 05:34:04 瀏覽:819
shell腳本累加 發布:2024-10-09 05:33:41 瀏覽:843