常用的Linux系统函数
文件I/O
open 打开文件
creat 创建文件 等同于open(const char * pathname, (O_CREAT|O_WRONLY|O_TRUNC))
close 关闭文件
write 写文件
read 读文件
lseek 移动文件的读写位置
fcntl 文件描述符操作:改变一个已打开的文件的属性
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
int main()
{
int fd, len;
char s[] = "Linux\n", buffer[23];
fd = open("/tmp/temp", O_WRONLY | O_CREAT | O_TRUNC);
printf("%d\n", (int)sizeof(s));
len = write(fd, s, strlen(s));
printf("%d\n", len);
close(fd);
fd = open("/tmp/temp", O_RDONLY);
len = read(fd, buffer, sizeof(buffer));
close(fd);
write(STDOUT_FILENO, buffer, len);
printf("%d\n", len);
return 0;
}
网络socket
socket 建立一个socket通信
bind 服务端绑定socket到端口
listen 服务端等待连接
accept 接受socket连接请求
connect 客户端连接服务器
/* server.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#define MAXLINE 80
#define SERV_PORT 8000
int main(void)
{
struct sockaddr_in servaddr, cliaddr;
socklen_t cliaddr_len;
int listenfd, connfd;
char buf[MAXLINE];
char str[INET_ADDRSTRLEN];
int i, n;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(listenfd, 20);
printf("Accepting connections ...\n");
while (1)
{
cliaddr_len = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
n = read(connfd, buf, MAXLINE);
printf("received from %s at PORT %d\n",
inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
ntohs(cliaddr.sin_port));
for (i = 0; i < n; i++)
buf[i] = toupper(buf[i]);
write(connfd, buf, n);
close(connfd);
}
}
/* client.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAXLINE 80
#define SERV_PORT 8000
int main(int argc, char *argv[])
{
struct sockaddr_in servaddr;
char buf[MAXLINE];
int sockfd, n;
char *str;
if (argc != 2)
{
fputs("usage: ./client message\n", stderr);
exit(1);
}
str = argv[1];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
write(sockfd, str, strlen(str));
n = read(sockfd, buf, MAXLINE);
printf("Response from server:\n");
write(STDOUT_FILENO, buf, n);
close(sockfd);
return 0;
}
进程原语
fork 创建一个新进程
getpid 获取进程id
getppid 获取父进程id
exec族 执行函数
wait 等待子进程终止并清理子进程PCB
waitpid 等待指定子进程终止并清理子进程PCB
进程间通信
管道:pipe 创建管道
FIFO有名管道:mkfifo 创建有名管道
共享内存:
shmget 获取共享内存
shmat 连接共享内存
shmctl 控制共享内存
shmdt 拆卸共享内存
线程原语
pthread_create创建线程
pthread_self获取调用线程tid
pthread_exit线程退出函数
pthread_join用该函数的线程将挂起等待,直到id为thread的线程终止
pthread_cancel在进程内某个线程可以取消另一个线程
pthread_detach线程也可以被置为detach状态,这样的线程一旦终止就立刻回收
它占用的所有资源,而不保留终止状态。
pthread_equal比较两个线程是否相等
线程同步
互斥量pthread_mutex_t、pthread_mutex_init、pthread_mutex_destroy、pthread_mutex_lock、pthread_mutex_trylock、pthread_mutex_unlock
读写锁pthread_rwlock_t、pthread_rwlock_init、pthread_rwlock_destroy、pthread_rwlock_rdlock、pthread_rwlock_wrlock、pthread_rwlock_tryrdlock、pthread_rwlock_trywrlock、pthread_rwlock_unlock
条件变量pthread_cond_t、pthread_cond_init、pthread_cond_destroy、pthread_cond_wait、pthread_cond_timedwait、pthread_cond_signal、pthread_cond_broadcast
信号量sem_t、sem_init、sem_wait、sem_trywait、sem_timedwait、sem_post、sem_destroy
上一篇: MySQL的常用函数
下一篇: Mysql的常用函数