導航:首頁 > 編程系統 > linuxc協程

linuxc協程

發布時間:2023-08-31 00:38:12

❶ 到底怎麼在linux里編寫c程序

在linux下通常使用gedit或vim直接編寫.c程序,然後通過gcc指令編譯。以Ubuntu系統為例,內詳細過程如下:

1、進入容桌面Temp文件

❷ 在Linux下如何開發C程序

在Linux開發環境下,GCC是進行C程序開發不可缺少的編譯工具。GCC是GNU C Compile的縮寫,是GNU/Linux系統下的標准C編譯器。雖然GCC沒有集成的開發環境,但堪稱是目前效率很高的C/C++編譯器。《linux就該這么學》非常值得您一看。Linux平台下C程序開發步驟如下:
1.利用編輯器把程序的源代碼編寫到一個文本文件中。
比如編輯test.c程序內容如下:
/*這是一個測試程序*/
#include<stdio.h>
int main(void)
{
printf("Hello Linux!");
}
2.用C編譯器GCC編譯連接,生成可執行文件。
$gcc test.c
編譯完成後,GCC會創建一個名為a.out的文件。如果想要指定輸出文件,可以使用選項-o,命令如下所示:
$gcc-o test1 test.c
這時可執行文件名就變為test1,而不是a.out。
3.用C調試器調試程序。
4.運行該可執行文件。 在此例中運行的文件是:
$./a.out 或者 test1
結果將得出:
Hello Linux!
除了編譯器外,Linux還提供了調試工具GDB和程序自動維護工具Make等支持C語言編程的輔助工具。如果想要了解GCC的所有使用說明,使用以下命令:
$man gcc

❸ linux怎麼運行c文件

linux怎麼運行c文件呢,下面就讓我們來看看吧。
1、安裝gccg++編譯工具。


以上就是小編的分享,希望能幫助的大家。
本文章基於ThinkpadE15品牌、centos7系統撰寫的。

❹ linux下的c/c++開發

我就是做LINUX下的C開發的。

准確的說,LINUX下C才是主要的開發語言,但是寫應用程序還是要用面向對象的,尤其是圖形界面的,不如QT和X和GTK等等。下面說的必須要求你C語言學的很好而且會使用LINUX的情況下。LINUX常用命令要熟悉。

要學LINUX下的C編程,先看一兩本LINUX下C的書比較好,這個你自己選擇,但是經典的書還是好一些,比如《LINUX程序設計》、《UNIX環境高級編程》,看這些的時候把書上的例子選擇一些自己編譯一下,哪怕是照著敲進去,事半功倍。

看完一本這樣的書,你對LINUX系統編程的知識就足夠了,這時候你可以開始閱讀一些LINUX下的源代碼來鍛煉和提升能力了,LINUX下有很多開源的軟體,你可以搜一下,應用程序也很多,但是最好的源代碼還是LINUX內核。

推薦一本簡單的內核書《linux內核完全剖析0.12》,這個講的是0.12內核,代碼量只有1萬多行的內核,現在的2.6估計一千萬,一輩子看不完。這個書講了很多X86體系結構的知識,這時理解內核必備的,匯編你要能看懂。

能看懂內核代碼了,就說明你至少不是初學者了,現在肯定能開發項目了,有了經驗後,恭喜你,你就成為一名LINUX程序員了。

再由這些基礎後,你就可以選擇一些具體的開放方向了,比如底層一點,驅動,協議等、或者嵌入式應用等、或者是QT等。不過這就是後話了,呵呵

我自己的一些體會,希望能對你有用。

❺ linux中C語言關於進程的創建

/*請解釋一下*/
while(((child=wait(&status))==-1)&(errno==EINTR));

這種的目的是父親進程等待子進程結束,回並回收子進程的資源,將子進程的退出狀態存答儲在status中,同時,返回該子進程的pid。
如果wait函數返回-1表示wait函數被其它情況打斷返回,並沒有等待到子進程結束,而同時判斷errno的值是不是EINTR(意思是讓你try again),那麼,讓進程繼續等待。因為這個錯誤並不是真正wait錯誤,而是被timeout時間等造成的,因此重新等待。而如果是其它情況,顯然是wait函數調用錯誤,即下面的if(child==-1),需要列印錯誤信息。『

//但你這句應該寫錯了。應該是邏輯與而不是位與操作。即
while(((child=wait(&status))==-1)&&(errno==EINTR));
/*請解釋一下*/
if(child==-1)

你對信號處理部分還需要努力。
另外介紹一本書《Linux高級程序設計 第3版》 上面講得很清楚。
有問題我們繼續交流,一起學習。

更多技術文章可以關注我的微博,名字:成都睿爾科技 。

❻ 在Linux系統中,如何運行一個C語言程序

1、打開kali linux的終端。創建一個文件並命名為test.c。在終端輸入:touch test.c。

❼ linux下c的兩個進程如何實現通信一個進程給另一個進程發送消息,另一個接受並顯示出來。求大神啊

linux中的進程通信分為三個部分:低級通信,管道通信和進程間通信IPC(inter process communication)。linux的低級通信主要用來傳遞進程的控制信號——文件鎖和軟中斷信號機制。linux的進程間通信IPC有三個部分——①信號量,②共享內存和③消息隊列。以下是我編寫的linux進程通信的C語言實現代碼。操作系統為redhat9.0,編輯器為vi,編譯器採用gcc。下面所有實現代碼均已經通過測試,運行無誤。

一.低級通信--信號通信

signal.c

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

/*捕捉到信號sig之後,執行預先預定的動作函數*/
void sig_alarm(int sig)
{
printf("---the signal received is %d. /n", sig);
signal(SIGINT, SIG_DFL); //SIGINT終端中斷信號,SIG_DFL:恢復默認行為,SIN_IGN:忽略信號
}

int main()
{
signal(SIGINT, sig_alarm);//捕捉終端中斷信號
while(1)
{
printf("waiting here!/n");
sleep(1);
}
return 0;
}

二.管道通信

pipe.c

#include <stdio.h>
#define BUFFER_SIZE 30

int main()
{
int x;
int fd[2];
char buf[BUFFER_SIZE];
char s[BUFFER_SIZE];
pipe(fd);//創建管道
while((x=fork())==-1);//創建管道失敗時,進入循環

/*進入子進程,子進程向管道中寫入一個字元串*/
if(x==0)
{
sprintf(buf,"This is an example of pipe!/n");
write(fd[1],buf,BUFFER_SIZE);
exit(0);
}

/*進入父進程,父進程從管道的另一端讀出剛才寫入的字元串*/
else
{
wait(0);//等待子進程結束
read(fd[0],s,BUFFER_SIZE);//讀出字元串,並將其儲存在char s[]中
printf("%s",s);//列印字元串
}
return 0;
}

三.進程間通信——IPC

①信號量通信

sem.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

/*聯合體變數*/
union semun
{
int val; //信號量初始值
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};

/*函數聲明,信號量定義*/
static int set_semvalue(void); //設置信號量
static void del_semvalue(void);//刪除信號量
static int semaphore_p(void); //執行P操作
static int semaphore_v(void); //執行V操作
static int sem_id; //信號量標識符

int main(int argc, char *argv[])
{
int i;
int pause_time;
char op_char = 'O';
srand((unsigned int)getpid());
sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT);//創建一個信號量,IPC_CREAT表示創建一個新的信號量

/*如果有參數,設置信號量,修改字元*/
if (argc > 1)
{
if (!set_semvalue())
{
fprintf(stderr, "Failed to initialize semaphore/n");
exit(EXIT_FAILURE);
}
op_char = 'X';
sleep(5);
}
for(i = 0; i < 10; i++)
{

/*執行P操作*/
if (!semaphore_p())
exit(EXIT_FAILURE);
printf("%c", op_char);
fflush(stdout);
pause_time = rand() % 3;
sleep(pause_time);
printf("%c", op_char);
fflush(stdout);

/*執行V操作*/
if (!semaphore_v())
exit(EXIT_FAILURE);
pause_time = rand() % 2;
sleep(pause_time);
}
printf("/n%d - finished/n", getpid());
if (argc > 1)
{
sleep(10);
del_semvalue(); //刪除信號量
}
exit(EXIT_SUCCESS);
}

/*設置信號量*/
static int set_semvalue(void)
{
union semun sem_union;
sem_union.val = 1;
if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
return(0);

return(1);
}

/*刪除信號量*/
static void del_semvalue(void)
{
union semun sem_union;
if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
fprintf(stderr, "Failed to delete semaphore/n");
}

/*執行P操作*/
static int semaphore_p(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1; /* P() */
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_p failed/n");
return(0);
}
return(1);
}

/*執行V操作*/
static int semaphore_v(void)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1; /* V() */
sem_b.sem_flg = SEM_UNDO;
if (semop(sem_id, &sem_b, 1) == -1)
{
fprintf(stderr, "semaphore_v failed/n");
return(0);
}
return(1);
}

②消息隊列通信

send.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_TEXT 512

/*用於消息收發的結構體--my_msg_type:消息類型,some_text:消息正文*/
struct my_msg_st
{
long int my_msg_type;
char some_text[MAX_TEXT];
};

int main()
{
int running = 1;//程序運行標識符
struct my_msg_st some_data;
int msgid;//消息隊列標識符
char buffer[BUFSIZ];

/*創建與接受者相同的消息隊列*/
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
if (msgid == -1)
{
fprintf(stderr, "msgget failed with error: %d/n", errno);
exit(EXIT_FAILURE);
}

/*向消息隊列中發送消息*/
while(running)
{
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
some_data.my_msg_type = 1;
strcpy(some_data.some_text, buffer);
if (msgsnd(msgid, (void *)&some_data, MAX_TEXT, 0) == -1)
{
fprintf(stderr, "msgsnd failed/n");
exit(EXIT_FAILURE);
}
if (strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
exit(EXIT_SUCCESS);
}

receive.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

/*用於消息收發的結構體--my_msg_type:消息類型,some_text:消息正文*/
struct my_msg_st
{
long int my_msg_type;
char some_text[BUFSIZ];
};

int main()
{
int running = 1;//程序運行標識符
int msgid; //消息隊列標識符
struct my_msg_st some_data;
long int msg_to_receive = 0;//接收消息的類型--0表示msgid隊列上的第一個消息

/*創建消息隊列*/
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
if (msgid == -1)
{
fprintf(stderr, "msgget failed with error: %d/n", errno);
exit(EXIT_FAILURE);
}

/*接收消息*/
while(running)
{
if (msgrcv(msgid, (void *)&some_data, BUFSIZ,msg_to_receive, 0) == -1)
{
fprintf(stderr, "msgrcv failed with error: %d/n", errno);
exit(EXIT_FAILURE);
}
printf("You wrote: %s", some_data.some_text);
if (strncmp(some_data.some_text, "end", 3) == 0)
{
running = 0;
}
}

/*刪除消息隊列*/
if (msgctl(msgid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "msgctl(IPC_RMID) failed/n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

③共享內存通信

share.h

#define TEXT_SZ 2048 //申請共享內存大小
struct shared_use_st
{
int written_by_you; //written_by_you為1時表示有數據寫入,為0時表示數據已經被消費者提走
char some_text[TEXT_SZ];
};

procer.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "share.h"

int main()
{
int running = 1; //程序運行標志位
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
int shmid; //共享內存標識符

/*創建共享內存*/
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed/n");
exit(EXIT_FAILURE);
}

/*將共享內存連接到一個進程的地址空間中*/
shared_memory = shmat(shmid, (void *)0, 0);//指向共享內存第一個位元組的指針
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed/n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X/n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;

/*生產者寫入數據*/
while(running)
{
while(shared_stuff->written_by_you == 1)
{
sleep(1);
printf("waiting for client.../n");
}
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer, TEXT_SZ);
shared_stuff->written_by_you = 1;
if (strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}

/*該函數用來將共享內存從當前進程中分離,僅使得當前進程不再能使用該共享內存*/
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed/n");
exit(EXIT_FAILURE);
}
printf("procer exit./n");
exit(EXIT_SUCCESS);
}

customer.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "share.h"

int main()
{
int running = 1;//程序運行標志位
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
int shmid; //共享內存標識符
srand((unsigned int)getpid());

/*創建共享內存*/
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed/n");
exit(EXIT_FAILURE);
}

/*將共享內存連接到一個進程的地址空間中*/
shared_memory = shmat(shmid, (void *)0, 0);//指向共享內存第一個位元組的指針
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed/n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X/n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->written_by_you = 0;

/*消費者讀取數據*/
while(running)
{
if (shared_stuff->written_by_you)
{
printf("You wrote: %s", shared_stuff->some_text);
sleep( rand() % 4 );
shared_stuff->written_by_you = 0;
if (strncmp(shared_stuff->some_text, "end", 3) == 0)
{
running = 0;
}
}
}

/*該函數用來將共享內存從當前進程中分離,僅使得當前進程不再能使用該共享內存*/
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed/n");
exit(EXIT_FAILURE);
}

/*將共享內存刪除,所有進程均不能再訪問該共享內存*/
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed/n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

摘自:http://blog.csdn.net/piaojun_pj/article/details/5943736

閱讀全文

與linuxc協程相關的資料

熱點內容
excel數據轉vcf文件 瀏覽:585
編程題如何左右翻轉 瀏覽:920
wp10微信安裝不了 瀏覽:793
微軟資料庫密碼怎麼改 瀏覽:627
我們是貴族什麼app可以看 瀏覽:65
哪個app認字免費 瀏覽:964
少兒編程線上教育如何 瀏覽:137
ps3升級檢測不到優盤升級文件 瀏覽:845
vs連接資料庫顯示數據 瀏覽:867
win10的設備和列印機 瀏覽:876
linux最新 瀏覽:970
蘋果6有時候自動重啟 瀏覽:851
nodejs處理500頁面 瀏覽:456
win10u盤格式化失敗怎麼辦啊 瀏覽:113
一個游戲測試你java基礎是否扎實 瀏覽:27
怎樣讓軟體不提示更新系統升級 瀏覽:653
1567密碼是什麼意思 瀏覽:759
手機和電視傳輸文件 瀏覽:844
iphone5美版怎麼樣 瀏覽:852
spss如何比較兩組數據的相關性 瀏覽:935

友情鏈接