Sie sind auf Seite 1von 10

Lesson #2

open.c simple open and read.


atr#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> int main() { int fd, rb; char buffer[4096]; fd = open("open.c", O_RDONLY, 0600); if (fd > -1) { /* -1 is used to save space for a terminating null * which is not required when using write(). */ rb = read(fd, buffer, sizeof(buffer)/*-1*/); if (rb > -1) { buffer[rb]='\0'; // NULL for printf() printf("%s\n", buffer); write(1, buffer, rb); } } close(fd); return 0;

// //

argv.c using argc and argv.


#include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int i; int pid; for (i=0; i<argc; ++i) printf("argument[%d]: %s\n", i, argv[i]); return 0; }

Lesson#3

fork.c fork example.


#include <unistd.h> #include <stdio.h> int main() { int pid; pid = fork(); if (0==pid) printf("son: pid: %d getpid(): %d getppid(): %d\n", pid, getpid(), getppid()); else { if (-1 == pid) perror("fork"); else printf("parent: pid: %d getpid(): %d getppid(): %d\n", pid, getpid(), getppid()); } return 0;

execlp.c, execvp.c exec library calls.


execlp:
#include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int pid; for (pid=0; pid<argc; ++pid) printf("param[%d]: %s\n", pid, argv[pid]); pid = fork(); if (0==pid) { if (argc > 1) execlp("ls", "ls", NULL); } else { if (-1 == pid) perror("fork"); else { // int stat; wait(&stat); printf("parent: pid: %d getpid(): %d getppid(): %d\n", pid, getpid(), getppid()); }

} return 0;

execvp:
#include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int i; int pid; for (i=0; i<argc; ++i) printf("argument[%d]: %s\n", i, argv[i]);

pid = fork(); if (0==pid) execvp("ls",argv); else { if (-1 == pid) perror("fork"); else { wait(NULL); printf("parent: pid: %d getpid(): %d getppid(): %d\n", pid, getpid(), getppid()); } }

return 0; }

pipe.c anonymous pipe.


#include #include #include #include #include #include #include #include <unistd.h> <sys/types.h> <sys/stat.h> <fcntl.h> <errno.h> <stdio.h> <stdlib.h> <sys/wait.h>

int main() { int fd[2]; char buff[4096]; int rb; if ( pipe(fd) ) { perror("pipe"); exit(-1); } if (fork()) // father { printf("parent pid: %d\n", getpid()); write(fd[1], "Hello son!\n", sizeof("Hello son!\n")); wait(NULL); } else // son { rb = read(fd[0], buff, sizeof(buff)); printf("son pid: %d, parent pid: %d\n", getpid(), getppid()); write(1, buff, rb); } close(fd[0]); close(fd[1]); return 0; }

Shared memory sample and semaphores


shm-simple.c
#include #include #include #include #include #include #include <sys/types.h> <sys/ipc.h> <sys/shm.h> <string.h> <signal.h> <stdio.h> <stdlib.h>

void cleanup(int); char str[]="shalom"; char *sharestr; int shmid; int main() { int s,i; shmid=shmget ( IPC_PRIVATE, strlen ( str ) + 1, 0600 ); printf("shmid: %d\n", shmid); sharestr=(char *)shmat ( shmid, 0 , 0); strcpy ( sharestr , str ); // copying "shalom" to sharestr before fork! printf ( "%s (%p)\n" , sharestr, sharestr); if ( fork()==0 ) // son { strcpy ( sharestr , "hello world!!!" ); // shared => visible to parent strcpy( str, "byebye"); // not shared => change local to son. printf ( "son:\n\tstr: %s\n\tsharestr: %s\n" , str, sharestr ); shmdt ( sharestr ); } else // father { wait ( &s ); printf ( "parent:\n\tstr: %s\n\tsharestr: %s\n" , str, sharestr ); shmdt ( sharestr ); shmctl ( shmid , IPC_RMID , 0 ); } return 0; }

semaphore.h
#ifndef __SEMAPHORE_H__ #define __SEMAPHORE_H__ void void void void init(int _nlocks); finilize(); lock(int locknum); unlock(int locknum);

#endif

semaphore.c
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <stdio.h> #include <stdlib.h> #include "semaphore.h" union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }; static int semid; static int nlocks; void err_exit(const char *str) { perror(str); exit(-1); } void init(int _nlocks) { int i; union semun sem_init; nlocks=_nlocks; semid = semget(IPC_PRIVATE, nlocks, 0600); if (semid < 0) err_exit("semget"); sem_init.array = (unsigned short *)malloc(sizeof(short)*nlocks); for (i=0; i<nlocks; ++i) sem_init.array[i] = 1; if ( semctl(semid, 0, SETALL, sem_init) < 0) { finilize(); err_exit("semctl"); } } void finilize() { if ( semctl(semid, 0, IPC_RMID) < 0) { finilize(); err_exit("semctl"); } /* /* /* /* Value for SETVAL */ Buffer for IPC_STAT, IPC_SET */ Array for GETALL, SETALL */ Buffer for IPC_INFO (Linux-specific) */

} void lock(int locknum) { struct sembuf sop; sop.sem_num = locknum; sop.sem_op = -1; sop.sem_flg = 0; if (semop(semid, &sop, 1) < 0) { finilize(); err_exit("semop"); } } void unlock(int locknum) { struct sembuf sop; sop.sem_num = locknum; sop.sem_op = 1; sop.sem_flg = 0; if (semop(semid, &sop, 1) < 0) { finilize(); err_exit("semop"); } }

sem_test.c

an example on how to use semaphore.h and semaphore.c.

Compile as follows:gcc sem_test.c semaphore.c


#include #include #include #include "semaphore.h" <stdio.h> <stdlib.h> <unistd.h>

int main() { init(2); fork(); fork(); lock(0); lock(1); printf("locking\n"); sleep(1); printf("unlocking\n"); unlock(1); unlock(0); sleep(4); printf("clearing...\n"); finilize(); return 0; }

General C examples (not OS)

Casting: cast.c
int main() { int i = 66000, j; short short_arr[2]; int *ptr; short_arr[0]=464; short_arr[1]=1; printf("%d\n", *((int*)short_arr)); write(1, &i, 4); for (j=0; j<2; j++) printf("%d ", ((short*)&i)[j]); printf("\n"); printf("int: %d, short: %d\n", i, (short)i); ptr = &i; printf("char: %d\n", *((unsigned char*)ptr)); return 0; }

//

// // //

Function pointers: func.c


#include <stdio.h> int b(int arg) { printf("in b, arg: %d\n", arg); return 0; } int a() { printf("in a\n"); return 0; }

int main() { int (*ptr)(int); ptr = b; ptr(5); return 0; }

Threads samples
Threads with simple mutex: threads.c
#include <pthread.h> #include <stdio.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *thread_main(void *arg) { printf("in thread %d\n", pthread_self()); pthread_mutex_lock(&mutex); printf("in thread %d locked\n", pthread_self()); sleep(1); pthread_mutex_unlock(&mutex); printf("in thread %d unlocked\n", pthread_self()); return NULL; } int main() { pthread_t tid[2]; pthread_create(tid, NULL, thread_main, NULL); pthread_create(tid+1, NULL, thread_main, NULL); pthread_exit(NULL); // // pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); printf("main finished\n"); return 0; }

Threads with conditional variables: threads2.c


#include <pthread.h> #include <stdio.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int count=0; void *thread_main(void *arg) { printf("in thread %d\n", pthread_self()); pthread_mutex_lock(&mutex); printf("Before cond %d\n", pthread_self()); sleep(1); if (!count) { ++count; pthread_cond_wait(&cond, &mutex); } else { pthread_cond_signal(&cond); } printf("After cond: %d\n", pthread_self()); pthread_mutex_unlock(&mutex); printf("in thread %d unlocked\n", pthread_self()); return NULL; } int main() { pthread_t tid[2]; pthread_create(tid, NULL, thread_main, NULL); pthread_create(tid+1, NULL, thread_main, NULL); pthread_exit(NULL); // // pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); printf("main finished\n"); return 0; }

Das könnte Ihnen auch gefallen