當前位置:首頁 » 編程語言 » linux下c語言小程序
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

linux下c語言小程序

發布時間: 2022-12-14 23:52:50

① 在linux下如何用c語言來寫一個socket編程的聊天小程序

你要去學習linux下進行網路編程的一些函數,比如socket(),listen,accept()等函數.再看點例子就會了

② C語言菜鳥自寫一個小程序在linux上編譯後提示Segmentation fault (core mped),求幫助

i != '\0' 改成 *i != '\0', 另一處j !='\0'也一樣

③ 在Linux下用C語言編程

4。守護進程的創建
如果你在DOS時代編寫過程序,那麼你也許知道在DOS下為了編寫一個常駐內存的程序我們要編寫多少代碼了.相反如果在Linux下編寫一個"常駐內存"的程序卻是很容易的.我們只要幾行代碼就可以做到. 實際上由於Linux是多任務操作系統,我們就是不編寫代碼也可以把一個程序放到後台去執行的.我們只要在命令後面加上&符號SHELL就會把我們的程序放到後台去運行的. 這里我們"開發"一個後台檢查郵件的程序.這個程序每個一個指定的時間回去檢查我們的郵箱,如果發現我們有郵件了,會不斷的報警(通過機箱上的小喇叭來發出聲音). 後面有這個函數的加強版本加強版本
後台進程的創建思想: 首先父進程創建一個子進程.然後子進程殺死父進程(是不是很無情?). 信號處理所有的工作由子進程來處理.

#include
#include
#include
#include
#include
#include
#include

/* Linux 的默任個人的郵箱地址是 /var/spool/mail/用戶的登錄名 */

#define MAIL "/var/spool/mail/hoyt"

/* 睡眠10秒鍾 */

#define SLEEP_TIME 10

main(void)
{
pid_t child;

if((child=fork())==-1)
{
printf("Fork Error:%s\n",strerror(errno));
exit(1);
}
else if(child>0)
while(1);
if(kill(getppid(),SIGTERM)==-1)
{
printf("Kill Parent Error:%s\n",strerror(errno));
exit(1);
}
{
int mailfd;

while(1)
{
if((mailfd=open(MAIL,O_RDONLY))!=-1)
{
fprintf(stderr,"%s","\007");
close(mailfd);
}
sleep(SLEEP_TIME);
}
}
}

你可以在默認的路徑下創建你的郵箱文件,然後測試一下這個程序.當然這個程序還有很多地方要改善的.我們後面會對這個小程序改善的,再看我的改善之前你可以嘗試自己改善一下.比如讓用戶指定郵相的路徑和睡眠時間等等.相信自己可以做到的.動手吧,勇敢的探險者.
好了進程一節的內容我們就先學到這里了.進程是一個非常重要的概念,許多的程序都會用子進程.創建一個子進程是每一個程序員的基本要求!

④ 高手幫忙寫一個linux下的c語言小程序

#include<stdio.h>

int main(int argc, char ** argv)
{
int i;
char a[20];
gets(a);
if (argc==1)
{
for(i=0;i<strlen(a);i++)
{
if(a[i] > 64 && a[i] < 91)
a[i]=a[i]+32;
putchar(a[i]);
}
putchar('\n');
}
for(i=1;i<argc;i++)
{
if(!strcmp(argv[i],"-c"))
{
puts(a);
exit(0);
}
if(!strcmp(argv[i],"-x"))
{
printf("error\n");
exit(1);
}
}
}
//在GCC中編譯好了就行了

⑤ 如何在linux下用c語言編寫一個能夠發送icmp報文的小程序

需要建立socket,參數是AF_INET,SOCK_RAW,IPPROTO_ICMP

自己構造ICMP數據包,sendto發送給某地址。

ICMP有多種,你可以發送type為13的時間戳請求。

然後調用recvfrom會收到type為14的timestampreply的IP包,

IP頭一般是20Bytes,裡麵包含srcIP,desIP還有TTL等。

IP包的數據就是返回ICMPtimestampreply報文,裡面有origTimestamp,recvTimestamp,transStamp,可以計算出時間。

可以參考網頁鏈接網頁鏈接

覺得有幫助可以注冊帳號,給他點個「星」

⑥ linux下c語言 遠程式控制制小程序

親……我也在研究信號和線程,你出現這個問題是因為內存是有限的,只允許一個程序運行,那麼另一個程序自然就堵塞在一邊了,如果被堵的不是主程序,主程序結束了自然就全結束了。建議你去看看互斥鎖 ,如果還不明白可以接著問……

⑦ 在linux下如何用c語言來寫一個socket編程的聊天小程序

源碼如下:
//chat_one.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>

int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chatone_fd;
int ret,i;
int len, msg_len;
fd_set fdset;

if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}

printf("server address is %s\n", argv[1]);

chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
inet_pton(AF_INET, argv[1], &chattwo.sin_addr.s_addr);

chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
chatone.sin_addr.s_addr = INADDR_ANY;

chatone_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chatone_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}

ret = bind(chatone_fd, (struct sockaddr *)&chatone, sizeof(chatone));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}

for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chatone_fd, &fdset);

if ( -1 == select ( chatone_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}

if ( FD_ISSET( chatone_fd, &fdset)){
recvfrom( chatone_fd, pmsg, 999, 0, NULL, 0);
printf("receive %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chattwo);
sendto( chatone_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chattwo, len);

printf("send %s\n", pmsg);
}
}

printf("sent %d packets\n", i);

close(chatone_fd);
return 0;
}
//chat_two.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>

int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chattwo_fd;
int ret,i;
int len, msg_len;
fd_set fdset;

if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}

printf("server address is %s\n", argv[1]);

chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
chattwo.sin_addr.s_addr = INADDR_ANY;

chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
inet_pton(AF_INET, argv[1], &chatone.sin_addr.s_addr);

chattwo_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chattwo_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}

ret = bind(chattwo_fd, (struct sockaddr *)&chattwo, sizeof(chattwo));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}

for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chattwo_fd, &fdset);

if ( -1 == select ( chattwo_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}

if ( FD_ISSET( chattwo_fd, &fdset)){
recvfrom( chattwo_fd, pmsg, 999, 0, NULL, 0);
printf("receive: %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chatone);
sendto( chattwo_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chatone, len);

printf("send %s\n", pmsg);
}
}

printf("sent %d packets\n", i);

close(chattwo_fd);
return 0;
}
編譯好這個兩個程序就可以進行簡單的通信了。

⑧ 用linux下的c語言讀取txt文件中的列數據

1.用fgets函數可以讀取文件中某行的數據,某列數據就必須一個一個讀入每行的第幾個字元,再存入到一個字元串當中。

2.常式:

#include<stdio.h>
#include<string.h>
voidmain()
{
chara[100],b[100],c[100];
inti=3,j=4,k=0;//第三行,第四列
FILE*fp=fopen("data.txt","r");
while(fgets(c,100,fp)){//讀入每行數據
i--;
if(i==0)strcpy(a,c);//讀到第三行數據
b[k++]=c[j-1];//把每行的那列字元拷到b中
}
b[k]=0;
printf("第%d行數據:%s ",i,a);
printf("第%d列數據:%s ",j,b);
fclose(fp);
}

⑨ linux系統下,c語言pthread多線程編程傳參問題

3個線程使用的都是同一個info

代碼 Info_t *info= (Info_t *)malloc(sizeof(Info_t));只創建了一個info

pthread_create(&threads[i],NULL,calMatrix,(void *)info); 三個線程使用的是同一個

我把你的代碼改了下:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

intmtc[3]={0};//resultmatrix

typedefstruct
{
intprank;
int*mta;
int*mtb;
}Info_t;

void*calMatrix(void*arg)
{
inti;
Info_t*info=(Info_t*)arg;
intprank=info->prank;
fprintf(stdout,"calMatrix:prankis%d ",prank);

for(i=0;i<3;i++)
mtc[prank]+=info->mta[i]*info->mtb[i];

returnNULL;
}

intmain(intargc,char**argv)
{
inti,j,k=0;
intmta[3][3];
intmtb[3]={1};
Info_t*info=(Info_t*)malloc(sizeof(Info_t)*3);

for(i=0;i<3;i++)
for(j=0;j<3;j++)
mta[i][j]=k++;
/*3threads*/
pthread_t*threads=(pthread_t*)malloc(sizeof(pthread_t)*3);
fprintf(stdout," ");fflush(stdout);
for(i=0;i<3;i++)
{
info[i].prank=i;
info[i].mta=mta[i];
info[i].mtb=mtb;
pthread_create(&threads[i],NULL,calMatrix,(void*)(&info[i]));
}
for(i=0;i<3;i++)
pthread_join(threads[i],NULL);

fprintf(stdout," ====thematrixresult==== ");
fflush(stdout);

for(i=0;i<3;i++)
{
fprintf(stdout,"mtc[%d]=%d ",i,mtc[i]);
fflush(stdout);
}
return0;
}

矩陣的計算我忘記了,你運行看看結果對不對

⑩ 怎麼編寫C語言程序,如:Helloworld的

代碼如下:

#include<stdio.h>

intmain(void)

{

printf("Hello,world!");

return0;

}

一、首先,打開我們的Visual C++ 6.0軟體,我使用的為中文版,軟體主界面如下圖所示:


(10)linux下c語言小程序擴展閱讀

C語言的基本構成:

1、數據類型。C的數據類型包括:整型、字元型、實型或浮點型(單精度和雙精度)、枚舉類型、數組類型、結構體類型、共用體類型、指針類型和空類型。

2、常量與變數。常量其值不可改變,符號常量名通常用大寫。變數是以某標識符為名字,其值可以改變的量。標識符是以字母或下劃線開頭的一串由字母、數字或下劃線構成的序列,請注意第一個字元必須為字母或下劃線,否則為不合法的變數名。變數在編譯時為其分配相應存儲單元。

3、數組。如果一個變數名後面跟著一個有數字的中括弧,這個聲明就是數組聲明。字元串也是一種數組。它們以ASCII的NULL作為數組的結束。要特別注意的是,方括內的索引值是從0算起的。

4、指針。

(1)、如果一個變數聲明時在前面使用 * 號,表明這是個指針型變數。換句話說,該變數存儲一個地址,而 *(此處特指單目運算符 * ,下同。C語言中另有 雙目運算符 *) 則是取內容操作符,意思是取這個內存地址里存儲的內容。指針是 C 語言區別於其他同時代高級語言的主要特徵之一

(2)、指針不僅可以是變數的地址,還可以是數組、數組元素、函數的地址。通過指針作為形式參數可以在函數的調用過程得到一個以上的返回值,不同於return(z)這樣的僅能得到一個返回值。

(3)指針是一把雙刃劍,許多操作可以通過指針自然的表達,但是不正確的或者過分的使用指針又會給程序帶來大量潛在的錯誤。

5、字元串。C語言的字元串其實就是以''字元結尾的char型數組,使用字元型並不需要引用庫,但是使用字元串就需要C標准庫裡面的一些用於對字元串進行操作的函數。它們不同於字元數組。使用這些函數需要引用頭文件<string.h>。

6、文件輸入/輸出在C語言中,輸入和輸出是經由標准庫中的一組函數來實現的。在ANSI C中,這些函數被定義在頭文件<stdio.h>;中。