Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

C - Programming in Linux

C - Programming
• #include <stdio.h>
#include “reciprocal.hpp”
int main (int argc, char **argv)
{
int i;
i = atoi (argv[1]);
printf (“The reciprocal of %d is %g\n”, i, reciprocal (i));
return 0;
}
Install and check GCC
• $ gcc --version
• More https://linux.die.net/man/1/gcc
Compiling
 Compiling a Single Source File
 gcc -c filedestination filesource
 Linking Object Files
 gcc –o –g -wall filedestination list-filesource
 Option of gcc/g++
 o: option represent to place output in file filedestination, the
default is to put an executable file in out extension (*.out)
 c option represent to compile or assemble the source files,
but do not link
 g option represent to produce debugging
 wall: warning
Using Libraries
 Archives
 gcc -o filedestination.a lis-filesource.o
 Shared Libraries
 gcc -c -fPIC lis-filesource.c
 gcc -shared -fPIC filedestination.so lis-filesource.o
Process
• Use <unistd.h>
• Looking at Processes
• Process IDs: getpid()
• Parent process IDs: getppid()
• Creating Processes
• System
• fork and exec
Process
• Signals
• use <signal.h>
• SIG_DFL, which specifies the default disposition for the signal.
• SIG_IGN, which specifies that the signal should be ignored.
• sig_atomic_t
• Process Termination
• kill (child_pid, SIGTERM);
• use <sys/types.h> and <signal.h>
• Wait, <sys/wait.h>
• Zombie Processes :exit
Process
• Other Methods
• int system(const char *string);
• int execl(const char *path, const char *arg, ...) int execv(const char *path, const char *argv[ ])
• int execlp(const char *lename, const char *arg, ...) int execvp(const char *lename, const char
*argv[ ])
• int execle(const char *path, const char *arg, ...,const char **env)
• int execve(const char *path, const char *argv[ ], const char **env)
Threads
• Use <pthread.h>
• Init: int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine), (void*) *argdata)
• Thread Creation
• pthread_t
• pthread_attr_t
• pthread_exit
• Passing Data to Threads
• Joining Threads: pthread_join
Threads
• Thread Return Values: void*
• More on Thread IDs
• pthread_self : the thread ID of the thread in which it is called.
• Compared with another thread ID using the pthread_equal function
• Thread Attributes
• pthread_attr_t
• pthread_attr_init
• pthread_attr_destroy
• Thread Cancellation
• pthread_cancel
• Cleanup Handlers
• pthread_cleanup_push
• pthread_cleanup_pop
Threads
• Synchronization and Critical Sections
• Race Conditions: job_queue
• Mutexes : pthread_mutex_t , pthread_mutex_init , pthread_mutex_lock, pthread_mutex_unlock
• Mutex Deadlocks: pthread_mutexattr_destroy
• Nonblocking Mutex Tests : pthread_mutex_trylock
• Semaphores for Threads:
• semaphore.h
• sem_t , sem_init
• sem_destroy
• sem_wait
• sem_trywait
• sem_getvalue
Threads
• Synchronization and Critical Sections
• Condition Variables
• pthread_cond_t
• pthread_cond_init
• pthread_cond_signal
• pthread_cond_wait
Example
• Display the ID of this process and the ID of the parent process (P1.c)
• Use fork to create Process (P2.c)
• Use fork to create Process and child Process (P3.c)
• Creates a number of child processes (passed in via the main argument),
prints the child processes' IDs and the parent process pending call (P4.c)
• Using SIGCHLD, SIG_IGN (P5.c)
• Using system (P6.c)
• Create Thread (T1.c)
• Create Thread using data (T2.c)
• Join Thread (T3.c)

You might also like