Linux 程序设计(第4版)笔记

1. make -f Makefile

testApp:test.o
	gcc -o testApp test.o

test.o:test.c
	gcc -c -o test.o test.c
	

2. open unlink

OPEN(2)                            Linux Programmer's Manual                           OPEN(2)

NAME
       open, creat - open and possibly create a file or device

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>

       int open(const char *pathname, int flags);
       int open(const char *pathname, int flags, mode_t mode);

       int creat(const char *pathname, mode_t mode);
       
UNLINK(2)                          Linux Programmer's Manual                         UNLINK(2)

NAME
       unlink - delete a name and possibly the file it refers to

SYNOPSIS
       #include <unistd.h>

       int unlink(const char *pathname);

DESCRIPTION
       unlink() deletes a name from the file system.  If that name was the last link to a file
       and no processes have the file open the file is deleted and the space it was  using  is
       made available for reuse.

       If  the name was the last link to a file but any processes still have the file open the
       file will remain in existence until the last file descriptor referring to it is closed.

       If the name referred to a symbolic link the link is removed.

       If the name referred to a socket, fifo or device the name for it is  removed  but  pro‐
       cesses which have the object open may continue to use it.

3. chmod

CHMOD(2)                           Linux Programmer's Manual                          CHMOD(2)

NAME
       chmod, fchmod - change permissions of a file

SYNOPSIS
       #include <sys/stat.h>

       int chmod(const char *path, mode_t mode);
       int fchmod(int fd, mode_t mode);

4. getopt

int getopt(int argc,char * const argv[ ],const char * optstring);

extern char * optarg;
extern int optind, opterr, optopt;

	// example
	while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)
    {
    	case 'a':
    	braek;
    	case '?':
    	braek;
    }

5. fork & exec

类型 如何形成 后续
孤儿进程 父进程已结束 | 子进程未结束 被 init 进程收养(主动调用 wait/waitpid)
僵尸进程 父进程未结束 | 子进程已结束 | 父进程未调用 wait / waitpid 进程表项得不到回收
守护进程 后台运行 | 无控制终端 服务程序常用形式
函数 行为 是否新建进程表项
exec 替换进程映像 | 原程序停止运行
fork 复制进程映像 | 原程序继续运行 是(有可能形成僵尸进程)
	// example
	pid_t pid = fork();
	
    if ( pid < 0 ) 
    {
    	// error  
	}
    else if ( pid == 0 ) {  
        // child process 
    }  
    else {  
        // parent process
        // wait()
    }  

6. signal kill alarm

SIGNAL(2)                          Linux Programmer's Manual                         SIGNAL(2)

NAME
       signal - ANSI C signal handling

SYNOPSIS
       #include <signal.h>

       typedef void (*sighandler_t)(int);

       sighandler_t signal(int signum, sighandler_t handler);

KILL(2)                            Linux Programmer's Manual                           KILL(2)

NAME
       kill - send signal to a process

SYNOPSIS
       #include <sys/types.h>
       #include <signal.h>

       int kill(pid_t pid, int sig);

NAME
       alarm - set an alarm clock for delivery of a signal

SYNOPSIS
       #include <unistd.h>

       unsigned int alarm(unsigned int seconds);


7. POSIX Thread

PTHREAD_CREATE(3)                  Linux Programmer's Manual                 PTHREAD_CREATE(3)

NAME
       pthread_create - create a new thread

SYNOPSIS
       #include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

       Compile and link with -pthread.

PTHREAD_EXIT(3)                    Linux Programmer's Manual                   PTHREAD_EXIT(3)

NAME
       pthread_exit - terminate calling thread

SYNOPSIS
       #include <pthread.h>

       void pthread_exit(void *retval);

       Compile and link with -pthread.

PTHREAD_JOIN(3)                    Linux Programmer's Manual                   PTHREAD_JOIN(3)

NAME
       pthread_join - join with a terminated thread

SYNOPSIS
       #include <pthread.h>

       int pthread_join(pthread_t thread, void **retval);

       Compile and link with -pthread.

8. Multi-thread Synchronization

SEM_INIT(3)                        Linux Programmer's Manual                       SEM_INIT(3)

NAME
       sem_init - initialize an unnamed semaphore

SYNOPSIS
       #include <semaphore.h>

       int sem_init(sem_t *sem, int pshared, unsigned int value);

       Link with -pthread.

PTHREAD_MUTEX_DESTROY(3P)          POSIX Programmer's Manual         PTHREAD_MUTEX_DESTROY(3P)

PROLOG
       This manual page is part of the POSIX Programmer's Manual.  The Linux implementation of
       this interface may differ (consult the corresponding Linux manual page for  details  of
       Linux behavior), or the interface may not be implemented on Linux.

NAME
       pthread_mutex_destroy, pthread_mutex_init - destroy and initialize a mutex

SYNOPSIS
       #include <pthread.h>

       int pthread_mutex_destroy(pthread_mutex_t *mutex);
       int pthread_mutex_init(pthread_mutex_t *restrict mutex,
              const pthread_mutexattr_t *restrict attr);
       pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

8. socket

SOCKET(2)                          Linux Programmer's Manual                         SOCKET(2)

NAME
       socket - create an endpoint for communication

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int socket(int domain, int type, int protocol);

SELECT(2)                          Linux Programmer's Manual                         SELECT(2)

NAME
       select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O multiplexing

SYNOPSIS
       /* According to POSIX.1-2001 */
       #include <sys/select.h>

       /* According to earlier standards */
       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       int select(int nfds, fd_set *readfds, fd_set *writefds,
                  fd_set *exceptfds, struct timeval *timeout);

猜你喜欢

转载自blog.csdn.net/qq_38408378/article/details/85711837
今日推荐