/**************************************************************************** * FUNCTION : main ****************************************************************************/ int main() { printf("Proc 2\n"); /* ## Semaphore - init ## */ int semid = init_semaphore(KEY_SEM_ACCESS_I2C_DEV, 1); if (semid == -1) { printf("ERROR: Init semaphore \n"); return -1; } printf("%s - semid = %d \n", PROCESS_NAME,semid); while(1){ printf("%s - wait the semaphore\n", PROCESS_NAME); if (SEM_P(semid) != 0) { perror(PROCESS_NAME); exit(EXIT_FAILURE); } printf("%s - enter critical section\n", PROCESS_NAME); sleep(2); printf("%s - exit critical section\n", PROCESS_NAME); printf("%s - signal the semaphore\n", PROCESS_NAME); SEM_V(semid); } return 0; }
/* init_itron --- ITRON の初期化を行う。 * */ static ER init_itron () { init_interrupt (); simple_init_console (); /* コンソールに文字を出力できるようにする */ pmem_init (); /* 物理メモリ管理機能の初期化 */ banner (); /* 立ち上げメッセージ出力 */ printf ("init_itron: start\n"); init_kalloc (); /* バイト単位のメモリ管理機能の初期化 */ init_semaphore (); /* セマフォの管理機能の初期化 */ init_msgbuf (); /* メッセージ管理機能の初期化 */ init_eventflag (); /* イベントフラグ管理機能の初期化 */ #ifdef notdef init_mpl (); /* メモリプール管理機能の初期化 */ simple_init_console (); /* コンソールに文字を出力できるようにする */ #endif init_task (); /* タスク管理機能の初期化 */ /* 1番目のタスクを初期化する。そしてそのタスクを以後の処 * 理で使用する。 */ init_task1 (); printf ("call init_timer\n"); init_timer (); /* インターバルタイマ機能の初期化 */ start_interval (); /* インターバルタイマの起動 */ init_io (); return (E_OK); }
int main(){ int shmflag=PROT|IPC_CREAT; int shmid; // ID du segment de mémoire partagée shmid = shmget(IPC_PRIVATE,5*sizeof(int),shmflag); // Création d'un segment de mémoire partagée de taille 5 entiers if(shmid == -1) perror("Création échouée"); else printf("Segment mémoire créé.\n"); int *idx_w; // Index d'écriture int *idx_r; // Index de lecture idx_w=shmat(shmid,0,shmflag); // On attache le segment mémoire à l'index d'écriture idx_r=shmat(shmid,0,shmflag); // On attache le segment mémoire à l'index de lecture pid_t fils; // Variables du fils int i, j=0; // Variables du père int k, l=0; // Création des sémaphores init_semaphore(); // Initialisation des sémaphores val_sem(sem_vide,5); // Au début, il y a 5 emplacements libres dans le buffer val_sem(sem_plein,0); // Au début, il n'y a pas de message en attente switch (fils=fork()){ case -1: perror("Probleme fork"); break; case 0: // Fils - PRODUCTEUR for (i = 1; i < 51; i++) // Pour afficher 50 valeurs { P(sem_vide); // On applique P au sémaphore comptant le nombre d'emplacement libres idx_w[j]=i; printf("Produit : %d\n", idx_w[j]); j++; j = j%5; // Le buffer est circulaire de taille 5, on accède uniquement aux cases 0 1 2 3 et 4 V(sem_plein); // On applique V au sémaphore comptant le nombre de messages en attente } shmdt(idx_w); // Détachement du segment mémoire break; default: // Père - CONSOMMATEUR for (k = 1; k < 51; k++) { P(sem_plein); // On applique P au sémaphore comptant le nombre de messages en attente printf("Consommé : %d\n", idx_r[l]); l++; l = l%5; // Le buffer est circulaire de taille 5, on accède uniquement aux cases 0 1 2 3 et 4 V(sem_vide); // On applique V au sémaphore comptant le nombre d'emplacement libres } wait(0); // On attend la fin du fils shmdt(idx_r); // Détachement du segment mémoire shmctl(shmid, IPC_RMID,0); // Destruction du segment mémoire printf("Segment mémoire supprimé.\n"); detruire_semaphore(); } return 0; }
rd_t open_buf_stream(char *buf) { struct buf_stream *env = malloc(sizeof(struct buf_stream)); if (!env) { printk("OOPS: Could not allocate space for buffer stream resource.\r\n"); return -1; } resource *new_r = create_new_resource(); if (!new_r) { printk("OOPS: Unable to allocate space for buffer stream resource.\r\n"); free(env); return -1; } env->buf = buf; new_r->env = env; new_r->writer = &buf_stream_write; new_r->swriter = NULL; new_r->reader = &buf_stream_read; new_r->closer = &buf_stream_close; new_r->sem = malloc(sizeof(semaphore)); if (new_r->sem) { init_semaphore(new_r->sem); } else { printk("OOPS: Unable to allocate memory for buffer stream semaphore.\r\n"); kfree(new_r); free(env); return -1; } return add_resource(curr_task->task, new_r); }
int main() { key_t key; int shmid; int* data; pid_t pid; int mutex; mutex = init_semaphore(); val_sem(mutex, 1); //récupération de la clé key = ftok("sem1.c", 11); if(key == -1) perror("error ftok"); //creation smp et allocation mémoire shmid=shmget(key,5*sizeof(int), 0666|IPC_CREAT|IPC_EXCL) ; if(shmid== -1) perror("Création échoue error shmid)") ; //get adresse smp E = shmat(shmid, (void*)0, 0); if(E == -1) perror("error shmat"); int i; int A; E = 0; //initialisation de l'entier a 0; pid = fork(); switch(pid){ case -1 : perror("Error fork"); exit(0); break; case 0 : //fils printf("Fils !"); for(i = 0; i < 100; i++) { P(mutex); A = E; usleep(rand()%80+20); A++; E = A; usleep(rand()%80+20); V(mutex); } exit(1); break; default : //père printf("Père !"); while(1) { i= (i+1)%5; A = rand()%100 } break; } }
int main(){ init_semaphore(); val_sem(1,2); P(2); sleep(5); V(2); detruire_semaphore(); return 0; }
int main(int argc, char *argv[]){ int idsem1; int val; val= atoi(argv[1]); idsem1 = init_semaphore(val); printf("idsem : %d\n",idsem1); exit(0); }
int tkn_rw_init(krwlock_t *rw) { int error = 0; LockTKN(); if ( rw->semid == 0 ) error = init_semaphore(rw); UnlockTKN(); return error; }
int init_game(t_env *env) { key_t key; char path[1024]; getcwd(path, sizeof(path)); key = ftok("./lemipc", 0); init_share(env, key); init_semaphore(env, key); place_me(env); return (0); }
int main() { int key = 123 ; int memsize = sizeof(int) ; int shmemId = create_shmem(key, memsize) ; if (shmemId == -1) { perror("shared segment creation error") ; exit(EXIT_FAILURE) ; } int* buffer = (int*) attach_shmem(shmemId) ; *buffer = 0 ; int emptySemKey = 100 ; int emptySemId = create_semaphore(emptySemKey) ; int fullSemKey = 101 ; int fullSemId = create_semaphore(fullSemKey) ; if ((emptySemId == -1) || (fullSemId == -1)) { perror("semaphore creation error") ; exit(EXIT_FAILURE) ; } init_semaphore(fullSemId, 0) ; init_semaphore(emptySemId, 1) ; while (*buffer >= 0) { down(emptySemId) ; if (!scanf("%d", buffer)) *buffer = -1 ; up(fullSemId) ; } remove_semaphore(fullSemId) ; remove_semaphore(emptySemId) ; detach_shmem(buffer) ; remove_shmem(shmemId) ; }
int main() { int sem_key=ftok("key.txt",'A'); if(0 > sem_key) { perror("Error in getting key! (or) \"key.txt\" File not found. Cause: "); return errno; } int semid=semget(sem_key,SEM_COUNT,IPC_CREAT|IPC_EXCL|0600); /* IPC_CREAT - used to create a new semaphore IPC_EXCL - Fail if semaphore is already created */ if(0 > semid) { perror("Error in creating semaphore! Cause:"); return errno; } init_semaphore(semid); printf("\n Free Resources at semaphore 0 : %d",semctl(semid,0,GETVAL)); /*Creating two threads */ pthread_t threads[THREAD_COUNT]; for(int i=0;i<THREAD_COUNT;i++) { pthread_create(&threads[i],NULL,thrfunction,&semid); } for(int i=0;i<THREAD_COUNT;i++) { pthread_join(threads[i],NULL); } if(semctl(semid,0,IPC_RMID) < 0) { perror("Could not delete semaphore. Cause:"); } return 0; }
int main(int argc, char **argv) { int fd; init_semaphore(); semSet(); init_shared(); if( (fd = open("debug.txt", O_CREAT | O_TRUNC, 0666)) < 0) { fprintf(stderr, "Cannot create a file\n"); exit(1); } close(fd); return 0; }
int main() { int shmid; int* ptr; int i = 0, A=0; /* init_semaphore(); printf("Initialisation du 2ème sémaphore à 1\n"); val_sem(2,1); printf("P(2)\n"); P(2); sleep(20); printf("V(2)\n"); V(2); detruire_semaphore(); */ // Creation et attachement d'un segment mémoire // On crée le segment en utilisant IPC_PRIVATE car le segment de mémoire est partagé entre un processus père et un processus fils shmid = shmget(IPC_PRIVATE, 100, IPC_CREAT | 0600 ); //on attache ce segment à l'espace virtuel du processus ptr = shmat(shmid, 0, 0); // Création du sémaphore init_semaphore(); // Initialisation du premier sémaphore à 1 (mutex) val_sem(1,1); // On initialise l'entier E à 0 en mémoire partagée ptr[0] = 0; // Création du processus fils pid_t pidChild = fork(); if (pidChild < 0) { // fork failed. perror("Fork of pidChild failed."); return -1; } else if (pidChild == 0) { // Processus fils for(i = 0 ; i<100 ; i++) { P(1); A = ptr[0]; //attendre entre 20 et 100ms (Utilisez les fonctions usleep(3) et rand(3V)) usleep(rand_a_b(20, 100)); A++; ptr[0] = A; V(1); usleep(rand_a_b(20, 100)); } shmdt(ptr); } else { //processus père for(i = 0 ; i<100 ; i++) { P(1); A = ptr[0]; //attendre entre 20 et 100ms (Utilisez les fonctions usleep(3) et rand(3V)) usleep(rand_a_b(20, 100)); A++; ptr[0] = A; V(1); usleep(rand_a_b(20, 100)); printf("%dème valeur de E : %d\n", i, ptr[0]); } //On attend la fin du processus fils if (waitpid(pidChild, 0, 0) == -1) { // Si echec du waitpid, envoie d'un signal d'interruption kill(pidChild, SIGKILL); perror("Wait child failed"); return -1; } printf("valeur finale de E : %d\n", ptr[0]); detruire_semaphore(); // on effectue le détachement du segment shmdt(ptr); // Suppression du segment de mémoire partagée shmctl(shmid, IPC_RMID, 0); } return 0; }
int main() { int erreur; erreur = init_semaphore(); int i = 0; int shmid; shmid = shmget(IPC_PRIVATE,100*sizeof(int),IPC_CREAT|0666); //Flag IPC_CREAT = Si il n'existe pas on le crée, 0666 = les droits if(shmid == -1) { perror("Creation du shm echoue\n"); exit(1); } //Attacher le segment partagé:fonction shmat int* T; T = (int *)shmat(shmid,NULL,0); T[0] = 0; if(T ==-1) { perror("Attachement echoue\n"); exit(1); } //Utiliser le segment comme un tableau pid_t fils; fils = fork(); if(fils == -1) exit(2); if(fils== 0) //Dans le fils { int a; for(i = 0;i<100;i++) { erreur = P(0); a = T[0]; usleep(20); a++; T[0] = a; usleep(32); erreur = V(0); } } else //dans le père { int a; for(i = 0;i<100;i++) { erreur = P(0); a = T[0]; usleep(16); a++; T[0] = a; usleep(22); printf("%d" , T[0]); erreur = V(0); } wait(NULL); printf("VALEUR %d", T[0]); } //Detacher le segment partagé shmdt(T); //detruire le segment partagé if(shmctl(shmid,IPC_RMID,0) == -1) { perror("Segment non détruit"); exit(1); } erreur = detruire_semaphore; return 0; }