还是继续这个项目。
在上一篇Linux下利用Shell使PHP并发采集淘宝产品中,采用shell将对PHP的调用推到后台执行,模拟多线程。
此方法有一致命缺点,只能人工预判每个程序执行时间。如果判断时间少于执行时间,则会生成大量进程,如果判断时间多于执行时间,则会浪费时间资源。
所以,在此我们采用C程序来控制并发数。
整体思路和用shell调用相似,只是把shell控制改成了C。
下面是C程序:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include "/usr/local/include/mysql/mysql.h" 6 #define MAX_COLUMN_LEN 32 7 #define THREAD_NUM 20//线程数 8 int threads = 0; 9 pthread_t thread[THREAD_NUM]; 10 pthread_mutex_t mut;//线程锁 11 int count=0,vod_count=0,number = 0; 12 int *goods_id[1000000]; 13 void *thread1(int thread_id) 14 { 15 int sleepsec; 16 while (number < count){; 17 char shell_cmd[50]; 18 printf("number:%d\tthread_id=%d\tid=%s\n", number, thread_id, goods_id[number]); 19 sprintf(shell_cmd, "/usr/local/bin/php /var/www/9384shop/cron/goodsupdate.php %s", goods_id[number]);//生成shell命令 20 system(shell_cmd);//调用shell 21 pthread_mutex_lock(&mut); 22 number++; 23 pthread_mutex_unlock(&mut); 24 } 25 pthread_exit(NULL); 26 } 27 28 void create_thread(void){ 29 int i,temp; 30 for (i = 0; i < THREAD_NUM; i++){ 31 if (thread[i] == 0){ 32 if ((temp = pthread_create(&thread[i], NULL, thread1, i)) != 0){ 33 } 34 else{ 35 threads++; 36 } 37 break; 38 } 39 } 40 sleep(1); 41 } 42 void thread_wait(void) 43 { 44 int i; 45 /*等待线程结束*/ 46 for (i = 0; i < THREAD_NUM; i++){ 47 if (thread[i] != 0) { 48 pthread_join(thread[i], NULL); 49 } 50 } 51 } 52 int main(int argc, char *argv[]){ 53 MYSQL my_connection; 54 MYSQL_RES *result; 55 MYSQL_ROW sql_row; 56 MYSQL_FIELD *fd; 57 char column[MAX_COLUMN_LEN][MAX_COLUMN_LEN]; 58 int res,flag; 59 mysql_init(&my_connection); 60 if (mysql_real_connect(&my_connection, "localhost" 61 , "root", "202.133", "shop", 3306, NULL, 0)){ 62 printf("connected to mysql.\n"); 63 res = mysql_query(&my_connection, "select id from s_goods where is_off_sale=0 order by id desc limit 1000000");//查询 64 printf("select id from s_goods where is_off_sale=0 order by id desc limit 1000000\n"); 65 if (!res){ 66 int i = 0, j; 67 result = mysql_store_result(&my_connection);//保存查询到的数据到result 68 printf("the result number is %lu\n", (unsigned long)mysql_num_rows(result)); 69 count = (unsigned long)mysql_num_rows(result); 70 while (sql_row = mysql_fetch_row(result))//获取具体的数据 71 { 72 goods_id[i] = (unsigned long)sql_row[0]; 73 i++; 74 } 75 } 76 mysql_close(&my_connection);//断开连接 77 while (threads < THREAD_NUM) 78 create_thread(); 79 thread_wait(); 80 }