Study overview this week
● System call
● Standard I/O Library
● Process Control
● Inter-process communication: pipelines, named pipelines, and message queues
● Basic signal operations
● Basic thread operations
The details are as follows:
● System call
Basic commands
# Who
Purpose: display the login user name, terminal name, and logon time
# Open
Purpose: open a file.
Header file # include <fcntl. h>
Prototype int open (char * Name, int how)
# Close
Purpose: close a file.
Header file: # include <unistd. h>
Prototype: int close (int fd)
# Read
Reads a specified number of data into the buffer zone.
# Include <unistd. h>
Ssize_t read (int fd, void * Buf, size_t count)
● Standard I/O Library
The following library functions are # include <stdio. h>
Enable and disable a stream
File * fopen (const char * path, const char * mode );
Int fclose (File * stream );
I/O of one character each time
Int GETC (File * FP)
Int fgetc (File * FP)
Int getchar (void)
Int putc (int c, file * FP)
Int fputc (int c, file * FP)
Int putchar (int c)
I/O of each row
Char * fgets (char * Buf, int N, file * FP)
Char * gets (char * BUF)
Int fputs (const char * STR, file * FP)
Int puts (const char * Str)
Data Block I/O
Size_t fread (void * PTR, size_t size, size_t nobj, file * FP)
Size_t fwrite (const void * PTR, size_t size, size_t nobj, file * FP)
Locate stream
Long ftell (File * FP)
Int fseek (File * FP, long offset, int whence)
Void rewind (File * FP)
Int fgetpos (File * FP, fpos_t * POS)
Int fsetpos (File * FP, fpos_t * POS)
Format output
Int printf (const char * format ,...)
Int fprintf (File * FP, const char * format ,...)
Int sprintf (char * Buf, const char * format ,...)
Int snprintf (char * Buf, size_t N, const char * format ,...)
Format input
Int scanf (const char * format ,...)
Int fscanf (File * FP, const char * format ,...)
Int sscanf (const char * Buf, const char * format ,...)
File stream Detection
Int feof (File * FP)
Int ferror (File * FP)
● Process Control
Fork function: Create a New Process
# Include <unistd. h>
Pid_t fork (void)
Vfork function: Create a New Process
# Include <unistd. h>
Pid_t vfork (void );
Exec Function
Int execl (const char * path, const char * arg0 ,... , (Char *) 0)
Int execv (const char * path, char * const argv [])
Int execle (const char * path, const char * arg0 ,... , (Char *) 0, char * const envp [])
Int execve (const char * path, char * const argv [], char * const envp [])
Int execlp (const char * filename, const char * arg0 ,..., (Char *) 0)
Int execvp (const char * filename, char * const argv [])
Wait and waitpid Functions
# Include <sys/Wait. H>
Pid_t wait (int * Status)
Pid_t waitpid (pid_t PID, int * status, int options)
Exit and _ exit Functions
# Include <stdlib. h>
Void exit (INT status)
Void _ exit (INT status)
Sleep Function
Unsigned int sleep (unsigned int Sec)
● Inter-process communication
Create pipe Pipeline
# Include <unistd. h>
Int pipe (INT filedes [2]);
Create a FIFO queue
# Include <sys/types. h>
# Include <sys/STAT. h>
Int mkfifo (const char * pathname, mode_t Mode)
Create, send, receive, and Control Messages for message queues
Int msgget (key_t key, int flag );
Key must use the ftok function to generate unique values.
Key_t ftok (char * pathname, int projid );
Int msgsnd (INT msqid, const void * PTR, size_t nbytes, int flag );
Struct msgbuf {
Long mtype; // Message Type
Char mtext [1] // Message Body
}
Ssize_t msgrcv (INT msqid, void * PTR, size_t nbytes, long type, int flag)
Int msgctl (INT msqid, int cmd, struct msqid_ds * BUF)
● Basic signal operations
Kill transmission signal
Int kill (pid_t PID, int sig)
Raise transmission signal
Int raise (INT sig)
Alarm signal clock
Unsigned int alarm (unsigned int seconds)
Pause suspension Signal
Int pause (void)
Signal installation signal function
# Include <signal. h>
Void (* signal (INT signo, void (* func) (INT)
The signal set function group has the following functions:
Initialize the signal set and set it to null.
Int sigemptyset (sigset_t * Set)
Initializes the signal set and sets the signal set to the set of all signals.
Int sigfillset (sigset_t * Set)
Add the signal signo to the signal set.
Int sigaddset (sigset_t * Set, int signo)
The signal signo is deleted from the signal set.
Int sigdelset (sigset_t * Set, int signo)
Query whether the signal is in the Signal Set
Int sigismember (sigset_t * Set, int signo)
The sigprocmask function is used to add or delete the specified signal set to the signal blocking set of the process.
Int sigprocmask (INT how, const sigset_t * Set, sigset_t * oset)
● Basic thread operations
Thread Creation
# Include <pthread. h>
Int pthread_creat (pthread_t * thread, pthread_att_t * ATTR, void * (* start_rtn) (void *), void * Arg)
Thread termination
# Include <pthread. h>
Void pthread_exit (void * rval_ptr );
Waiting for thread termination
Void pthread_join (pthread_t thread, void ** rval_ptr)
Request to cancel other threads in the same process
# Include <pthread. h>
Void pthread_cancel (pthread_t tid)
Thread cleanup Handler
# Include <pthread. h>
Void pthread_cleanup_push (void (* RTN) (void *), void * Arg );
Void pthread_cleanup_pop (INT execute );
Define mutex lock
# Include <pthread. h>
Pthread_t mutex;
Initializes a mutex lock.
# Include <pthread. h>
Int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * ATTR)
Clear a mutex lock
Int pthread_mutex_destroy (pthread_mutex_t * mutex)
Lock mutex lock
# Include <pthread. h>
Int pthread_mutex_lock (pthread_mutex_t * mutex)
Int pthread_mutex_trylock (pthread_mutex_t * mutex)
Unlock a mutex lock
Int pthread_mutex_unlock (pthread_mutex_t * mutex)
Semaphores
PV operation
Sem_init is used to create a semaphore and initialize its value.
# Include <semaphore. h>
Int sem_init (sem_t * SEM, int pshared, unsigned int value)
SEM definition sem_t SEM;
P operation
# Include <pthread. h>
Int sem_wait (sem_t * SEM)
Int sem_trywait (sem_t * SEM)
V Operation
# Include <pthread. h>
Int sem_post (sem_t * SEM)
Int sem_getvalue (sem_t * SEM)
Int sem_destroy (sem_t * SEM)
Main exercises:
Standard I/O:
Exercise 1:
1. Define a utmp struct variable
2. Open the utmp File
3. Read the content of a struct variable from the utmp file to.
4. display the contents of a. ut_user
5. Repeat Step 1 to read the utmp file.
Exercise 2:
1. fread and fwrite: Use fopen, fread, and fclose to implement the who command.
2. fseek: Use fseek to locate the last and fourth user information of the utmp file and start reading.
5. fsetpos and fgetpos. Use fsetpos to implement the functions in step 2.
3. sprintf atoi: Use sprint atoi to convert data types (integer to string, character to integer), display or use GDB debugging to observe data.
4. fprintf and fscanf: Use fprint to save the output information in the WHO command to the file.
6. Implement conversion between IP address (string) and integer data (four int type data): Use sscanf and sprintf
Char * Host = "192.168.220.5 ";
Int P1;
Int P2;
Int P3;
Int P4;
After conversion: p1 = 192; P2 = 168; P3 = 220; P4 = 5
Implementation: (1) ip> INTEGER (2) integer> ip
Process exercise:
1. Use fork to create another process in the main process
Output "in main process" in the main process and its PID Number.
Output "in sub process" in the sub-process and its PID Number.
2. Create another process with vfork in the main process, change the global variables in the sub-process, observe the output in the main process, and compare it with the sub-process created with fork.
3. Use execl to call the "ls-a" command.
Example: execl ("/bin/ls", "ls", "-l", "-a", (char *) 0 );
Char argv [] = {"ls", "-l", "-a", (char *) 0 };
Execv ("/bin/ls", argv );
4. Write a simple shell script and call it with execl.
5. Use the wait and waitpid functions to verify that the main process exits after the child process;
6. create two sub-processes in the main process. The first sub-process waits for 2 seconds, and the second sub-process waits for 3 seconds. The main process uses waitpid to wait for the two sub-processes to exit, output some prompt information to observe the results.
7. Compare exit (0) and _ exit (0) to observe the execution results of the following two programs:
Int main ()
{Printf ("this is a test/N ")
Printf ("test exit fun ");
Exit (0 );
}
Int main ()
{Printf ("this is a test/N ")
Printf ("test exit fun ");
_ Exit (0 );
}
Inter-process communication
1. Use pipe to implement "LS-Al | more ". Knowledge points involved: pipe, dup2, and fork. Program name DUP. c
2. FIFO enables chat between two terminals on the local machine. Knowledge points involved: mkfiof, fork, open, close, read, write, and fgets. Program name chat1.c
Chat2.c
3. MQ enables chat between two terminals on the local machine. Knowledge point involved: msgget msgsnd msgrcv msgctl fork fgets. program name msg_chat.c
Signal exercises:
Exercise: fork creates two processes. The parent process uses the system to call kill () to send 16 and 17 Soft Interrupt signals to the two child processes respectively. In the child process, use the signal function to set the signal processing function, pause waits until the signal ends and dosomething ends. Program name: signal_ex.c.
Thread exercises:
Repository, producer, and consumer exercises. knowledge points involved: mutex lock and PV operation.
Program name: semmphore. C.