日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

linux web高级编程,寒假学习 第16.17天 (linux 高级编程)

發布時間:2023/11/27 生活经验 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux web高级编程,寒假学习 第16.17天 (linux 高级编程) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寒假學習 第16.17天 (linux 高級編程) 筆記 總結

一、進程的基本控制(進程的同步)

1.進程的常見控制函數

pause ? sleep/usleep

atexit ? on_exit

int atexit(void (*function)(void)); ? ?//注冊終止函數(即main執行結束后調用的函數)

int on_exit(void (*function)(int , void *), void *arg); //跟atexit差不多,只不過函數可以帶參數

#include

#include

#include

void fun()

{

printf("over!\n");

}

int main(int argc, const char *argv[])

{

atexit(fun); //注冊,

printf("Processs!\n");

return 0;

}

2.進程與文件鎖(鎖有共享鎖跟強制鎖,內核編譯不同)

多進程下面文件的讀寫是共享的。

怎么知道一個文件正在被另外進程讀寫?

就是要使用文件鎖。

API:

fcntl (文件鎖受內核參數影響)

對文件加鎖

int fcntl(int fd, int cmd, ... /* arg */ );

返回0加鎖成功,-1加鎖失敗。

fd 加鎖的文件符號

cmd 鎖的操作方式 F_SETLK(如果已經加鎖就異常返回) ?F_UNLK ?F_SETLCKW(已經加鎖,者、則阻塞等待等到解鎖)

... struct flock* 類型

struct flock {

...

short l_type; /* Type of lock: F_RDLCK,

F_WRLCK, F_UNLCK */

short l_whence; /* How to interpret l_start:

SEEK_SET, SEEK_CUR, SEEK_END */

off_t l_start; /* Starting offset for lock */

off_t l_len; /* Number of bytes to lock */

pid_t l_pid; /* PID of process blocking our lock

(F_GETLK only) */

...

};

例子:

setlock.c

#include

#include

#include

#include

int main(int argc, const char *argv[])

{

int fd;

struct flock lk;

int r;

fd=open("test.txt",O_RDWR);

if(fd==-1) printf("error:%m\n"),exit(-1);

lk.l_type=F_WRLCK;

lk.l_whence=SEEK_SET;

lk.l_start=5;

lk.l_len=10;

r=fcntl(fd,F_SETLK,&lk);

if(r==0)

printf("加鎖成功!\n");

else

printf("加鎖失敗!\n");

while(1);

return 0;

}

getlock.c

#include

#include

#include

#include

int main(int argc, const char *argv[])

{

int fd;

struct flock lk={0}; //一定要初始化0

int r;

fd=open("test.txt",O_RDWR);

if(fd==-1) printf("error:%m\n"),exit(-1);

r=fcntl(fd,F_GETLK,&lk);

if(lk.l_type=F_WRLCK){

printf("寫鎖!\n");

}

printf("pid:%d,start:%d, len:%d\n",r,lk.l_pid,lk.l_start,lk.l_len);

return 0;

}

鎖也是一個進程可以共享的信息

二、信號

1.信號的作用

流程:

信號發給操作系統,操作系統查找這個信號是否注冊,如果注冊系統就調用函數

沒有注冊則采用缺省處理(一般是調用打印信號提示,并終止程序)

為了解決進程之間通信難。

作用:通知其他進程響應。(其實就是進程之間一種通信機制)

一般接受信號的進程會馬上停止(軟中斷),并且調用信號的處理函數(默認的處理函數,或者用戶的處理函數)。

例子:

2.信號的發送與安裝

kill ? -s ?信號 ?進程ID ? ? ? ? //向指定的進程發送信號

kill -l ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//可以看所有的信號 ?ctrl+D 就相當于發送信號2(即SIGINT)

例子:

#include

#include

#include

void handle(int s)

{

printf("信息發生\n");

}

int main(int argc, const char *argv[])

{

signal(SIGINT,handle); //注冊信號

while(1)

{

printf("進程在執行!\n");

sleep(1);

}

return 0;

}當kill -s 2 pid 時(或直接ctrl+C)就會輸出 ? “信號發送”

其中?SIGKILL與?SIGSTOP信號是不能被處理的

int kill(pid_t pid, int sig); ?//發送信號

pid:如果>0 ?發送到指定進程

如果=0 ?發送信號到該進程所在進程組的所有進程

如果=-1 發送給除init (1)之外的所有進程

如果<0 ? 發送給指定的進程組(組ID就是 它的絕對值)

例子:

#include

#include

#include

int main(int argc, const char *argv[])

{

while(1)

{

kill(4184,SIGINT);

sleep(2);

}

return 0;

}

3.信號的應用(實現多任務)

延時器 timeout

信號:SIGALRM

信號發送函數:unsigned int alarm(unsigned int seconds); //自向本進程發出

例子:

#include

#include

void deal(int s)

{

printf("起床!\n");

}

int main(int argc, const char *argv[])

{

signal(SIGALRM,deal);

alarm(5);

while(1)

{

printf("AAAAA\n");

sleep(1);

}

return 0;

}

定時器

int getitimer(int which, struct itimerval *curr_value);

int setitimer(int which, //計時方式 ?ITIMER_REAL真實時間 ?ITIMER_VIRTUAL程序暫用cpu的時間 ??ITIMER_PROF 進程跟系統的混合時間

const struct itimerval *new_value, ?//定時器的時間參數

struct itimerval *old_value); ? //返回原來設置的定時器,如果NULL 者不返回。

(set interval timter)

struct itimerval {

struct timeval it_interval;// 延時時間

struct timeval it_value; ? //間隔時間

};

struct timeval {

time_t ? ? ?tv_sec; ? ? ? ? ? ?//秒

suseconds_t tv_usec; ? //毫秒

};

例子:

#include

#include

#include

void deal(int s)

{

printf("起床!\n");

}

int main(int argc, const char *argv[])

{

struct itimerval val={0};

signal(SIGALRM,deal);

val.it_value.tv_sec=3; //sec跟usec都為0的表無窮時間,不會觸發

val.it_interval.tv_sec=1;

setitimer(ITIMER_REAL,&val,0);

while(1)

{

printf("AAAAA\n");

sleep(1);

}

return 0;

}3秒鐘過后每隔1秒鐘顯示 “起床了”。

http://www.dengb.com/ASPjc/729417.htmlwww.dengb.comtruehttp://www.dengb.com/ASPjc/729417.htmlTechArticle寒假學習 第16.17天 (linux 高級編程) 筆記 總結 一、進程的基本控制(進程的同步) 1.進程的常見控制函數 pause sleep/usleep atexit on_exit int...

總結

以上是生活随笔為你收集整理的linux web高级编程,寒假学习 第16.17天 (linux 高级编程)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。