當前位置:編程學習大全網 - 源碼下載 - CGI 程序能使用***享內存嗎

CGI 程序能使用***享內存嗎

LINUX 下用 C 寫了壹個 CGI 程序,想用這個 CGI 程序實現***享內存的功能

發現直接用 ROOT 登陸服務器,在服務器上面執行這個 CGI 程序,可以使用***享內存,而用 IE 調用這個 CGI 發現無法***享內存,是否因為權限的問題?

妳的意思是說在在服務器用IE可以,但是在其他電腦上用IE不可以?

在服務器直接運行這個程序可以的,在其它任何機器上面用 IE 調用都不行

把妳的創建***享內存的代碼貼出來。

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <errno.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/types.h>

#define KEYCOUNT 5678

#define SIZECOUNT (1 * 1024)

int main()

{

int shm_id = 0;

int i = 0;

int *lineCount = 0;

int lineCountTmp = 0;

shm_id = shmget(KEYCOUNT,SIZECOUNT,IPC_CREAT);

if(shm_id == -1)

{

perror("shmget error");

return 0;

}

lineCount = (int*)shmat(shm_id,NULL,0);

*lineCount = 1;

if (shmdt(lineCount) == -1)

perror(" detach error ");

shmctl(shm_id,IPC_RMID,NULL);

return 0;

}

源代碼如上

問題還是沒有解決,請各位看看,我最後發現,用 C 寫的 CGI 程序,只要裏面有對指針操作的,都會造成這個 CGI 無法運行

樓上的兄弟,這個問題困擾我很久,雖然在 C 板帖了,但是還是沒有人能解決這個問題,所以只能在這裏也帖壹個,希望能解決,否則真的要讓我丟飯碗的

妳是用apache+cgi(c)的方式吧?,在後臺直接運行cgi可以是在root下的吧?有兩個方法來驗證這個出在哪裏,第壹個就是直接修改apache的權限,默認是nobody,具體怎麽改可以找資料,我以前寫過怎麽改了,現在找不到了,妳可以自己找找。另壹個方法如果妳覺得麻煩可以創建壹個普通用戶,然後用這個普通用戶在後臺執行這個cgi,看行不行。當妳有了結果就知道問題在哪了。等妳好消息:)

shm_id = shmget(KEYCOUNT,SIZECOUNT,IPC_CREAT);

改為:

shm_id = shmget(KEYCOUNT,SIZECOUNT,IPC_CREAT | IPC_EXCL | 0666);

試試。

感覺妳寫的頭兩個參數很不規範。

顯示是權限的問題, limlzm(凡葉) 提供的方法是對的, 但不不全面

下面附的是可執行的代碼, 自已比較下吧.

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/types.h>

#include <unistd.h>

#define KEYCOUNT (key_t)56789

#define SIZECOUNT (1 * 1024)

int main()

{

int shm_id = 0;

int i = 0;

int *lineCount = 0;

int lineCountTmp = 0;

printf("Content-Type: text/html\n\n");

printf("<html>\n<body>\n");

shm_id = shmget(KEYCOUNT,SIZECOUNT,IPC_CREAT|IPC_EXCL|06);

if(shm_id == -1)

{

shm_id = shmget(KEYCOUNT,SIZECOUNT,IPC_CREAT|06);

if(shm_id == -1)

{

perror("shmget error");

return 0;

}

}

lineCount = (int*)shmat(shm_id,NULL,0);

if ((int*)-1 == lineCount)

{

perror("shmgat error");

return 0;

}

*lineCount = 1;

printf("lineCount = %d<br>\n", *lineCount);

if (shmdt(lineCount) == -1)

perror(" detach error ");

//shmctl(shm_id,IPC_RMID,NULL);

printf("execute OK.\n");

printf("</body>\n</html>\n");

return 0;

}

多謝樓上幾位的幫助,用各位的方法我已經解決了問題,最後就是權限的問題

  • 上一篇:界面生成源代碼
  • 下一篇:暗黑3附魔效果怎麽排列?
  • copyright 2024編程學習大全網