Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Reading compiler errors ls2.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token In file included from /usr/include/stdio.h:75,

Similar presentations


Presentation on theme: "1 Reading compiler errors ls2.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token In file included from /usr/include/stdio.h:75,"— Presentation transcript:

1 1 Reading compiler errors ls2.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token In file included from /usr/include/stdio.h:75, from ls2.c:12: /usr/include/libio.h:332: error: expected specifier-qualifier-list before ‘size_t’ /usr/include/libio.h:364: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/libio.h:373: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/libio.h:493: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_IO_sgetn’ In file included from ls2.c:12: /usr/include/stdio.h:314: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/stdio.h:321: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/stdio.h:363: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/stdio.h:365: error: format string argument not a string type /usr/include/stdio.h:367: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/stdio.h:680: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread’ /usr/include/stdio.h:686: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite’

2 2 Next Week's Presentation Put your updated version of life.c on your wiki page In class explain the changes you made and describe the library features you used Demonstrate your program.

3 3 Threads Lightweight process share memory context, same heap, file descriptors, etc Different stack, instruction pointers, so each thread can execute a separate program. The operating system handles context switching between threads relieving the user of doing it. Best used with asynchronous event handling

4 4 Simple Thread Program #include /* standard I/O routines */ #include /* pthread functions and data structures */ /* function to be executed by the new thread */ void* do_loop(void* data) { int i; /* counter, to print numbers */ int j; /* counter, for delay */ int me = *((int*)data); /* thread identifying number */ for (i=0; i<10; i++) { for (j=0; j<500000; j++) /* delay loop */ ; printf("'%d' - Got '%d'\n", me, i); } /* terminate the thread */ pthread_exit(NULL); } /* like any C program, program's execution begins in main */ int main(int argc, char* argv[]) { int thr_id; /* thread ID for the newly created thread */ pthread_t p_thread; /* thread's structure */ int a = 1; /* thread 1 identifying number */ int b = 2; /* thread 2 identifying number */ /* create a new thread that will execute 'do_loop()' */ thr_id = pthread_create(&p_thread, NULL, do_loop, (void*)&a); /* run 'do_loop()' in the main thread as well */ do_loop((void*)&b); /* NOT REACHED */ return 0; }

5 5 Threads Compiling: cc thread.c -o thread -lpthread Synchronizing with a Mutex: A mutex is like a door to a section of code that only one thread can execute at a time. To use the code a thread locks the mutex and when it is finished with the code it unlocks it. If the mutex is already locked the thread will wait at the mutex until it is unlocked. When the mutex is unlocked a waiting thread (one that previously tried to lock the mutex) will gain access to the code and relock the mutex

6 6 Mutex Example #include /* standard I/O routines */ #include /* pthread functions and data structures */ /* global mutex for our program. assignment initializes it */ pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER; /* function to be executed by the new thread */ void* do_loop(void* data) { int rc; /* contain mutex lock/unlock results */ int i;/* counter, to print numbers */ int j;/* counter, for delay */ int me = *((int*)data); /* thread identifying number */ rc = pthread_mutex_lock(&a_mutex); for (i=0; i<10; i++) { for (j=0; j<500000; j++) /* delay loop ; printf("'%d' - Got '%d'\n", me, i); } rc = pthread_mutex_unlock(&a_mutex); /* terminate the thread */ pthread_exit(NULL); } /* like any C program, program's execution begins in main */ int main(int argc, char* argv[]) { int thr_id; /* thread ID for the newly created thread */ pthread_t p_thread; /* thread's structure */ int a = 1; /* thread 1 identifying number */ int b = 2; /* thread 2 identifying number */ /* create a new thread that will execute 'do_loop()' */ thr_id = pthread_create(&p_thread, NULL, do_loop, (void*)&a); /* run 'do_loop()' in the main thread as well */ do_loop((void*)&b); /* NOT REACHED */ return 0; }

7 7 Pipes NAME pipe ­ create pipe SYNOPSIS #include int pipe(int filedes[2]); DESCRIPTION pipe creates a pair of file descriptors, pointing to a pipe inode, and places them in the array pointed to by filedes. filedes[0] is for reading, filedes[1] is for writing. RETURN VALUE On success, zero is returned. On error, ­1 is returned, and errno is set appropriately.

8 8 Simple Pipe #include int main() { int len, i,apipe[2];/* two file descriptors */ char buf[BUFSIZ]; /* for reading end */ if (pipe(apipe)==-1){ perror("could not make pipe");exit(1);} printf("Got a pipe! It is file descriptors: { %d %d }\n", apipe[0], apipe[1]); while ( gets(buf) ){ /* get next line */ len = strlen( buf ); if ( write( apipe[1], buf, len) != len ){ /* send */ perror("writing to pipe"); /* down */ break;} /* pipe */ for ( i = 0 ; i<len ; i++ ) buf[i] = 'X' ; len = read( apipe[0], buf, BUFSIZ ) ; /* read */ if (len == -1){ perror("reading from pipe"); break;} if ( write( 1, buf, len ) != len ){ /* send */ perror("writing to stdout"); /* to */ break;} /* stdout */ write( 1, "\n", 1 ); }


Download ppt "1 Reading compiler errors ls2.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token In file included from /usr/include/stdio.h:75,"

Similar presentations


Ads by Google