博文记录:
有关fork()函数的返回值以及子进程和父进程问题,详见:
https://www.cnblogs.com/yuanshuang/p/5571127.html
有关锁的属性pthread_mutexattr_t问题,详见:
https://www.cnblogs.com/tianrks/p/10785577.html
进程间通信——互斥锁和条件变量,详见:
https://zhuanlan.zhihu.com/p/106143527
进程间共享内存的使用,详见:
https://blog.csdn.net/CLZHIT/article/details/104859183
Linux下“锁”的事儿——互斥锁、自旋锁、条件变量,详见:
https://www.cnblogs.com/tgycoder/p/5442040.html
Q1:通过设置线程锁属性,用mutex实现两个进程各加2千万,最终实现4千万。
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/time.h>
#include <strings.h>
#include <syslog.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/msg.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <sys/uio.h>
#include <sys/sendfile.h>
#define ARGS_CHECK(argc, val) {if(argc!=val) {printf("error args\n"); return -1;}}
#define ERROR_CHECK(ret, retval, funcName) {if(ret == retval) {perror(funcName); return -1;}}
#define THREAD_ERROR_CHECK(ret,funcName) {if(ret!=0) {printf("%s:%s\n",funcName,strerror(ret));return -1;}}
#define CHILD_THREAD_ERROR_CHECK(ret,funcName) {if(ret!=0) {printf("%s:%s\n",funcName,strerror(ret));return (void*)-1;}}
#endif
#include "func.h"
#include <iostream>
using namespace std;
#define N 20000000
class TestData
{
public:
inline void InitValue() { val = 0; }
inline void ProcessFunc() { val++; }
inline int GetValue() { return val; }
inline pthread_mutex_t& GetMutexLock() { return mutex; }
private:
int val;
pthread_mutex_t mutex;
};
int main()
{
int shmid = shmget(1000, 1<<20, IPC_CREAT | 0600);
if(shmid == -1)
{
cout << "shmget occur error" << endl;
}
TestData* ptr = static_cast<TestData*>(shmat(shmid, nullptr, 0));
ptr->InitValue();
pthread_mutexattr_t mattr;
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
int ret = pthread_mutex_init(&ptr->GetMutexLock(), &mattr);
if(ret != 0)
{
cout << "pthread_mutex_init occur error" << endl;
}
struct timeval start, end;
gettimeofday(&start, nullptr);
pid_t pid;
pid = fork();
if(pid < 0)
{
cout << "fork occur error" << endl;
}
else if(pid == 0)
{
cout << "I am child process, my process id is " << getpid() << endl;
for(int i = 0; i < N; i++)
{
pthread_mutex_lock(&ptr->GetMutexLock());
ptr->ProcessFunc();
pthread_mutex_unlock(&ptr->GetMutexLock());
}
}
else
{
cout << "I am parent process, my process id is " << getpid() << endl;
for(int i = 0; i < N; i++)
{
pthread_mutex_lock(&ptr->GetMutexLock());
ptr->ProcessFunc();
pthread_mutex_unlock(&ptr->GetMutexLock());
}
wait(nullptr);
gettimeofday(&end, nullptr);
cout << "process result = " << ptr->GetValue() << ", use time = "
<< (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec << endl;
}
return 0;
}