Skip to content

Commit

Permalink
code in class 9-11
Browse files Browse the repository at this point in the history
  • Loading branch information
forhappy committed Dec 6, 2011
1 parent 6377205 commit 0fdd474
Show file tree
Hide file tree
Showing 15 changed files with 533 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Code/class-10/futex/futex.c
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

22 changes: 22 additions & 0 deletions Code/class-10/futex/futex.h
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

94 changes: 94 additions & 0 deletions Code/class-11/PC.c
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 added Code/class-11/log.txt
Binary file not shown.
Binary file added Code/class-11/sem-example
Binary file not shown.
Binary file added Code/class-11/sem-example-good
Binary file not shown.
90 changes: 90 additions & 0 deletions Code/class-11/sem-example-good.c
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);
}
68 changes: 68 additions & 0 deletions Code/class-11/sem-example.c
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 added Code/class-11/test-condvar
Binary file not shown.
Loading

0 comments on commit 0fdd474

Please sign in to comment.