-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
533 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* futex.c | ||
* by WN @ Oct. 22, 2010 | ||
*/ | ||
|
||
#include <linux/kernel.h> | ||
#include <linux/futex.h> | ||
|
||
#include <xasm/asm_syscall.h> | ||
#include <xasm/futex.h> | ||
|
||
int | ||
futex_wait(void * addr, int val) | ||
{ | ||
return INTERNAL_SYSCALL_int80(futex, 6, | ||
(uintptr_t)addr, FUTEX_WAIT_PRIVATE, val, NULL, NULL, NULL); | ||
} | ||
|
||
int | ||
futex_wake(void * addr, int nr) | ||
{ | ||
return INTERNAL_SYSCALL_int80(futex, 6, | ||
(uintptr_t)addr, FUTEX_WAKE_PRIVATE, nr, NULL, NULL, 0); | ||
} | ||
|
||
// vim:ts=4:sw=4 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* xasm/futex.h | ||
* by WN @ Oct. 20, 2010 | ||
*/ | ||
|
||
/* wrapper of correponding futex system calls */ | ||
|
||
#ifndef __X86_FUTEX_H | ||
#define __X86_FUTEX_H | ||
|
||
#include <defs.h> | ||
|
||
extern int | ||
futex_wait(void * addr, int val); | ||
|
||
extern int | ||
futex_wake(void * addr, int nr); | ||
|
||
#endif | ||
|
||
// vim:ts=4:sw=4 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* ============================================================================= | ||
* | ||
* Filename: PC.c | ||
* | ||
* Description: producer && consumer. | ||
* | ||
* Version: 0.0.1 | ||
* Created: 12/06/2011 10:28:09 AM | ||
* Revision: r1 | ||
* Compiler: gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 | ||
* | ||
* Author: Fu Haiping (forhappy), [email protected] | ||
* Company: ICT ( Institute Of Computing Technology, CAS ) | ||
* | ||
* ============================================================================= | ||
*/ | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include </usr/include/semaphore.h> | ||
|
||
#define BUFF_SIZE 5 /* total number of slots */ | ||
#define NP 3 /* total number of producers */ | ||
#define NC 3 /* total number of consumers */ | ||
#define NITERS 4 /* number of items produced/consumed */ | ||
|
||
typedef struct { | ||
int buf[BUFF_SIZE]; /* shared var */ | ||
int in; /* buf[in%BUFF_SIZE] is the first empty slot */ | ||
int out; /* buf[out%BUFF_SIZE] is the first full slot */ | ||
sem_t full; /* keep track of the number of full spots */ | ||
sem_t empty; /* keep track of the number of empty spots */ | ||
sem_t mutex; /* enforce mutual exclusion to shared data */ | ||
} sbuf_t; | ||
|
||
sbuf_t shared; | ||
|
||
void *Producer(void *arg) | ||
{ | ||
int i, item, index; | ||
|
||
index = (int)arg; | ||
|
||
for (i=0; i < NITERS; i++) { | ||
|
||
/* Produce item */ | ||
item = i; | ||
|
||
/* Prepare to write item to buf */ | ||
|
||
/* If there are no empty slots, wait */ | ||
sem_wait(&shared.empty); | ||
/* If another thread uses the buffer, wait */ | ||
sem_wait(&shared.mutex); | ||
shared.buf[shared.in] = item; | ||
shared.in = (shared.in+1)%BUFF_SIZE; | ||
printf("[P%d] Producing %d ...\n", index, item); fflush(stdout); | ||
/* Release the buffer */ | ||
sem_post(&shared.mutex); | ||
/* Increment the number of full slots */ | ||
sem_post(&shared.full); | ||
|
||
/* Interleave producer and consumer execution */ | ||
if (i % 2 == 1) sleep(1); | ||
} | ||
return NULL; | ||
} | ||
|
||
void *Consumer(void *arg) | ||
{ | ||
/* Fill in the code here */ | ||
} | ||
|
||
int main() | ||
{ | ||
pthread_t idP, idC; | ||
int index; | ||
|
||
sem_init(&shared.full, 0, 0); | ||
sem_init(&shared.empty, 0, BUFF_SIZE); | ||
|
||
/* Insert code here to initialize mutex*/ | ||
|
||
for (index = 0; index < NP; index++) | ||
{ | ||
/* Create a new producer */ | ||
pthread_create(&idP, NULL, Producer, (void*)index); | ||
} | ||
|
||
/* Insert code here to create NC consumers */ | ||
|
||
pthread_exit(NULL); | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* ============================================================================= | ||
* | ||
* Filename: sem-example-good.c | ||
* | ||
* Description: sem good example | ||
* | ||
* Version: 0.0.1 | ||
* Created: 12/06/2011 10:32:51 AM | ||
* Revision: r1 | ||
* Compiler: gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 | ||
* | ||
* Author: Fu Haiping (forhappy), [email protected] | ||
* Company: ICT ( Institute Of Computing Technology, CAS ) | ||
* | ||
* ============================================================================= | ||
*/ | ||
#include <semaphore.h> | ||
#include <stdio.h> | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
|
||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <sys/mman.h> | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int fd, i,count=0,nloop=10,zero=0,*ptr; | ||
sem_t *mutex; | ||
int shm; | ||
|
||
//open a file and map it into memory | ||
if ((shm = shm_open("myshm", O_RDWR | O_CREAT, S_IRWXU)) < 0) { | ||
perror("shm_open"); | ||
exit(1); | ||
} | ||
|
||
if ( ftruncate(shm, sizeof(sem_t)) < 0 ) { | ||
perror("ftruncate"); | ||
exit(1); | ||
} | ||
|
||
if ((mutex = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, | ||
MAP_SHARED, shm, 0)) == MAP_FAILED) { | ||
perror("mmap"); | ||
exit(1); | ||
} | ||
|
||
if (sem_init(mutex, 1, 1) < 0) { | ||
perror("semaphore initialization"); | ||
exit(1); | ||
} | ||
#if 0 | ||
fd = open("log.txt",O_RDWR|O_CREAT,S_IRWXU); | ||
write(fd,&zero,sizeof(int)); | ||
ptr = mmap(NULL,sizeof(int),PROT_READ |PROT_WRITE,MAP_SHARED,fd,0); | ||
close(fd); | ||
|
||
/* create, initialize semaphore */ | ||
if( sem_init(&mutex,1,1) < 0) | ||
{ | ||
perror("semaphore initilization"); | ||
exit(0); | ||
} | ||
#endif | ||
if (fork() == 0) { /* child process*/ | ||
for (i = 0; i < nloop; i++) { | ||
sem_wait(mutex); | ||
printf("child entered crititical section: %d\n", (*ptr)++); | ||
sleep(2); | ||
printf("child leaving critical section\n"); | ||
sem_post(mutex); | ||
sleep(1); | ||
} | ||
exit(0); | ||
} | ||
/* back to parent process */ | ||
for (i = 0; i < nloop; i++) { | ||
sem_wait(mutex); | ||
printf("parent entered critical section: %d\n", (*ptr)++); | ||
sleep(2); | ||
printf("parent leaving critical section\n"); | ||
sem_post(mutex); | ||
sleep(1); | ||
} | ||
exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* ============================================================================= | ||
* | ||
* Filename: sem-example.c | ||
* | ||
* Description: sem example | ||
* | ||
* Version: 0.0.1 | ||
* Created: 12/06/2011 10:30:44 AM | ||
* Revision: r1 | ||
* Compiler: gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 | ||
* | ||
* Author: Fu Haiping (forhappy), [email protected] | ||
* Company: ICT ( Institute Of Computing Technology, CAS ) | ||
* | ||
* ============================================================================= | ||
*/ | ||
#include <semaphore.h> | ||
#include <stdio.h> | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
|
||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <sys/mman.h> | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int fd, i,count=0,nloop=10,zero=0,*ptr; | ||
sem_t mutex; | ||
|
||
//open a file and map it into memory | ||
|
||
fd = open("log.txt",O_RDWR|O_CREAT,S_IRWXU); | ||
write(fd,&zero,sizeof(int)); | ||
ptr = mmap(NULL,sizeof(int),PROT_READ |PROT_WRITE,MAP_SHARED,fd,0); | ||
close(fd); | ||
|
||
/* create, initialize semaphore */ | ||
if( sem_init(&mutex,1,1) < 0) | ||
{ | ||
perror("semaphore initilization"); | ||
exit(0); | ||
} | ||
if (fork() == 0) { /* child process*/ | ||
for (i = 0; i < nloop; i++) { | ||
sem_wait(&mutex); | ||
printf("child entered crititical section: %d\n", (*ptr)++); | ||
sleep(2); | ||
printf("child leaving critical section\n"); | ||
sem_post(&mutex); | ||
sleep(1); | ||
} | ||
exit(0); | ||
} | ||
/* back to parent process */ | ||
for (i = 0; i < nloop; i++) { | ||
sem_wait(&mutex); | ||
printf("parent entered critical section: %d\n", (*ptr)++); | ||
sleep(2); | ||
printf("parent leaving critical section\n"); | ||
sem_post(&mutex); | ||
sleep(1); | ||
} | ||
exit(0); | ||
} |
Binary file not shown.
Oops, something went wrong.