| | |
| | | DIRS = common hcnetdisk_wrapper netdisk |
| | | DIRS = common netdisk |
| | | |
| | | all: |
| | | for i in $(DIRS); do \ |
| | |
| | | #include <sys/stat.h> |
| | | #include <sys/sem.h> |
| | | #include <sys/shm.h> |
| | | #include <sys/file.h> |
| | | #include <fcntl.h> |
| | | #include <sys/mman.h> |
| | | #include <errno.h> |
| | |
| | | #define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH) |
| | | |
| | | |
| | | #define min(a,b) ((a) < (b) ? (a) : (b)) |
| | | #define max(a,b) ((a) > (b) ? (a) : (b)) |
| | | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| | | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| | | |
| | | |
| | | |
| | |
| | | ROOT=.. |
| | | LDLIBS+=-Wl,-rpath=../hclib:../hclib/HCNetSDKCom:../common |
| | | LDDIR += -L$(ROOT)/hcnetdisk_wrapper -L../hclib -L../hclib/HCNetSDKCom |
| | | LDLIBS += -lusgcommon -lhcnetsdk -lhpr -lHCCore $(EXTRALIBS) |
| | | LDLIBS += -lhcnetsdk -lhpr -lHCCore $(EXTRALIBS) |
| | | LIB_NETDISK = libnetdisk.a |
| | | DLIB_NETDISK = libnetdisk.so |
| | | PLATFORM=$(shell $(ROOT)/systype.sh) |
| | |
| | | #ifndef __NETDISK_H__ |
| | | #define __NETDISK_H__ |
| | | #include "usg_common.h" |
| | | #include "usg_typedef.h" |
| | | |
| | | typedef struct Netdisk_EnvConfig { |
| | | std::string libpath; |
| | |
| | | |
| | | virtual int downloadByTime(Netdisk_DownloadRequest &request, std::vector<std::string> *files) = 0; |
| | | |
| | | void saveLoginInfo(); |
| | | |
| | | int getStartChannel() {return startChannel;} |
| | | |
| | | int getMaxChannels() {return maxChannels;} |
| | | |
| | | }; |
| | | |
| | | |
| | | #endif |
New file |
| | |
| | | #include "netdisk.h" |
| | | |
| | | Netdisk::saveLoginInfo() { |
| | | int fd = open("./login_info.data", O_CREAT | O_APPEND | O_RDWR, FILE_MODE); |
| | | } |
| | |
| | | # |
| | | # Makefile for common library. |
| | | # |
| | | ROOT=.. |
| | | EXTRALIBS+= |
| | | INCLUDE+= |
| | | LIBCOMMON=$(ROOT)/common/libusgcommon.a |
| | | LDLIBS+=-L../hclib -L../hclib/HCNetSDKCom -Wl,-rpath=../hclib:../hclib/HCNetSDKCom \ |
| | | -lhcnetsdk -lhpr -lHCCore |
| | | |
| | | # -lAudioRender -lPlayCtrl -lSuperRender \ |
| | | # -lcrypto -lNPQos -lssl -lz \ |
| | | # -lanalyzedata -lHCDisplay -lHCPlayBack -lSystemTransform \ |
| | | # -lHCAlarm -lHCGeneralCfgMgr -lHCPreview \ |
| | | # -lHCCoreDevCfg -lHCIndustry -lHCVoiceTalk -libStreamTransClient |
| | | |
| | | |
| | | # LDLIBS+=libAudioRender.so libHCCore.so libhpr.so libPlayCtrl.so libSuperRender.so \ |
| | | # libcrypto.so ./libhcnetsdk.so libNPQos.so libssl.so libz.so \ |
| | | # HCNetSDKCom/libanalyzedata.so HCNetSDKCom/libHCGeneralCfgMgr.so HCNetSDKCom/libHCVoiceTalk.so \ |
| | | # HCNetSDKCom/libHCAlarm.so HCNetSDKCom/libHCIndustry.so HCNetSDKCom/libiconv2.so \ |
| | | # HCNetSDKCom/libHCCoreDevCfg.so HCNetSDKCom/libHCPlayBack.so HCNetSDKCom/libStreamTransClient.so \ |
| | | # HCNetSDKCom/libHCDisplay.so HCNetSDKCom/libHCPreview.so HCNetSDKCom/libSystemTransform.so |
| | | |
| | | |
| | | LDLIBS+=-Wl,-rpath=../hclib:../hclib/HCNetSDKCom:../common |
| | | LDDIR += -L$(ROOT)/hcnetdisk_wrapper -L../hclib -L../hclib/HCNetSDKCom |
| | | LDLIBS += -lhcnetsdk -lhpr -lHCCore -lpthread |
| | | LIB_NETDISK = libnetdisk.a |
| | | DLIB_NETDISK = libnetdisk.so |
| | | PLATFORM=$(shell $(ROOT)/systype.sh) |
| | | include $(ROOT)/Make.defines.$(PLATFORM) |
| | | |
| | | |
| | | PROGS = download1 channel |
| | | |
| | | all: test_file test_io |
| | | |
| | | |
| | | |
| | | all: $(PROGS) |
| | | |
| | | |
| | | |
| | | # 如果包A 引用包B, B 要放在 A 后面 |
| | | # channel: $(LIBCOMMON) |
| | | #test_right_walk: $(ROOT)/algorithm/graph.c $(ROOT)/algorithm/IndirectAlg.c $(LIBCOMMON) |
| | | test_right_walk2: test_right_walk2.c |
| | | #g++ -std=c++11 -mcx16 -Wall -DLINUX -D_GNU_SOURCE test_right_walk2.c -o $@ -lalg -lusgcommon |
| | | g++ -std=c++11 test_right_walk2.c -o test_right_walk2 -lalg -lusgcommon -lpthread |
| | | test_file: test_file.c |
| | | |
| | | clean: |
| | | rm -f $(PROGS) $(TEMPFILES) *.o |
| | | rm -f test *.o a.out core temp.* *.a *.so |
| | | |
| | | |
| | | $(LIBCOMMON): |
| | | (cd $(ROOT)/common && $(MAKE)) |
| | | include $(ROOT)/Make.common.inc |
New file |
| | |
| | | #include "usg_common.h" |
| | | #include "usg_typedef.h" |
| | | #include <sys/file.h> |
| | | #include <fcntl.h> |
| | | |
| | | int open_file(char *filename) { |
| | | int fd = open(filename, O_CREAT | O_APPEND | O_RDWR , FILE_MODE); |
| | | return fd; |
| | | } |
| | | |
| | | void write_to_file_with_no_lock(int fd, int start, int end) { |
| | | int i; |
| | | char buf[1024]; |
| | | for(i = start; i< end; i++) { |
| | | sprintf(buf, "%d\n", i); |
| | | if (write(fd, buf, strlen(buf)) == -1) { |
| | | err_exit(errno, "write_to_file"); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | void write_to_file_with_lock(int fd, int start, int end) { |
| | | int i; |
| | | char buf[1024]; |
| | | for(i = start; i< end; i++) { |
| | | sprintf(buf, "%d\n", i); |
| | | |
| | | if (flock(fd , LOCK_EX) == -1) { |
| | | err_exit(errno, "LOCK_EX"); |
| | | } |
| | | if (write(fd, buf, strlen(buf)) == -1) { |
| | | err_exit(errno, "write_to_file"); |
| | | } |
| | | if (flock(fd, LOCK_UN) == -1) |
| | | err_exit(errno, "LOCK_UN"); |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | void write_to_file(int fd, int start, int end) { |
| | | |
| | | write_to_file_with_no_lock(fd, start, end); |
| | | //write_to_file_with_lock(fd, start, end); |
| | | } |
| | | |
| | | void read_from_file(int fd) { |
| | | lseek(fd, 0, SEEK_SET); |
| | | char buf[1024]; |
| | | while(read( fd, buf, 1024) > 0) { |
| | | printf("%s", buf); |
| | | } |
| | | |
| | | } |
| | | |
| | | int main() { |
| | | int fd = open_file("test.txt"); |
| | | int status, i = 0, processors = 4, scale = 100; |
| | | pid_t productors[processors], consumers[processors]; |
| | | pid_t pid; |
| | | |
| | | fd = open_file("stand.txt"); |
| | | write_to_file_with_no_lock(fd, 0, processors * scale); |
| | | read_from_file(fd); |
| | | |
| | | for ( i = 0; i < processors; i++) { |
| | | if ((productors[i] = fork()) == 0) /* Child runs user job */ |
| | | { |
| | | fd = open_file("test.txt"); |
| | | int start = i * scale, end = i * scale + scale; |
| | | printf("======start=%d, end=%d\n", start, end); |
| | | write_to_file(fd, start, end); |
| | | sleep(1); |
| | | exit(0); |
| | | } |
| | | } |
| | | |
| | | |
| | | // for ( i = 0; i < processors; i++) { |
| | | // if ((consumers[i] = fork()) == 0) /* Child runs user job */ |
| | | // { |
| | | |
| | | // // char cmd[1000]; |
| | | // // char *argv[] = {"productor", start, end, NULL }; |
| | | // const char *cmd = "./consumer >> c.txt"; |
| | | // if (execl("/bin/bash", "bash", "-c", cmd, 0) < 0) |
| | | // { |
| | | // err_exit(errno, "consumers execve"); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | while ((pid = waitpid(-1, &status, 0)) > 0) { |
| | | if(WIFEXITED(status)) { |
| | | //fprintf(stderr, "child %d terminated normally with exit status=%d\n", pid, WEXITSTATUS(status)); |
| | | }else |
| | | fprintf(stderr, "child %d terminated abnormally\n", pid); |
| | | } |
| | | |
| | | if (errno != ECHILD) |
| | | perror("waitpid error"); |
| | | |
| | | close(fd); |
| | | } |
| | | |
| | | |
| | | |
New file |
| | |
| | | |
| | | #include "usg_common.h" |
| | | #include "usg_typedef.h" |
| | | #include <iostream> // not needed for many systems |
| | | #include <fstream> |
| | | #include <string> |
| | | |
| | | using namespace std; |
| | | |
| | | |
| | | // void write_to_file_with_no_lock(int fd, int start, int end) { |
| | | // int i; |
| | | // char buf[1024]; |
| | | // for(i = start; i< end; i++) { |
| | | // sprintf(buf, "%d\n", i); |
| | | // if (write(fd, buf, strlen(buf)) == -1) { |
| | | // err_exit(errno, "write_to_file"); |
| | | // } |
| | | |
| | | // } |
| | | |
| | | // } |
| | | |
| | | // void write_to_file_with_lock(int fd, int start, int end) { |
| | | // int i; |
| | | // char buf[1024]; |
| | | // for(i = start; i< end; i++) { |
| | | // sprintf(buf, "%d\n", i); |
| | | |
| | | // if (flock(fd , LOCK_EX) == -1) { |
| | | // err_exit(errno, "LOCK_EX"); |
| | | // } |
| | | // if (write(fd, buf, strlen(buf)) == -1) { |
| | | // err_exit(errno, "write_to_file"); |
| | | // } |
| | | // if (flock(fd, LOCK_UN) == -1) |
| | | // err_exit(errno, "LOCK_UN"); |
| | | |
| | | // } |
| | | |
| | | // } |
| | | |
| | | |
| | | void write_to_file(ofstream &fout, int start, int end) { |
| | | int i; |
| | | for(i = start; i< end; i++) { |
| | | fout << i << endl; |
| | | |
| | | } |
| | | //write_to_file_with_lock(fd, start, end); |
| | | } |
| | | |
| | | void read_from_file(ifstream &fin) { |
| | | fin.seekg(0); |
| | | char buf[1024]; |
| | | while(fin.getline(buf, 1024)) { |
| | | printf("%s\n", buf); |
| | | } |
| | | |
| | | } |
| | | |
| | | int main() { |
| | | ofstream fout; |
| | | ifstream fin("stand.txt"); |
| | | |
| | | |
| | | |
| | | int status, i = 0, processors = 4, scale = 100000; |
| | | pid_t productors[processors], consumers[processors]; |
| | | pid_t pid; |
| | | |
| | | fout.open("stand.txt", ios_base::app); |
| | | |
| | | write_to_file(fout, 0, processors * scale); |
| | | |
| | | fout.close(); |
| | | //read_from_file(fin); |
| | | |
| | | fout.open("test.txt", ios_base::out | ios_base::app); |
| | | |
| | | for ( i = 0; i < processors; i++) { |
| | | if ((productors[i] = fork()) == 0) /* Child runs user job */ |
| | | { |
| | | int start = i * scale, end = i * scale + scale; |
| | | printf("======start=%d, end=%d\n", start, end); |
| | | write_to_file(fout, start, end); |
| | | exit(0); |
| | | } |
| | | } |
| | | |
| | | |
| | | while ((pid = waitpid(-1, &status, 0)) > 0) { |
| | | if(WIFEXITED(status)) { |
| | | //fprintf(stderr, "child %d terminated normally with exit status=%d\n", pid, WEXITSTATUS(status)); |
| | | }else |
| | | fprintf(stderr, "child %d terminated abnormally\n", pid); |
| | | } |
| | | |
| | | if (errno != ECHILD) |
| | | perror("waitpid error"); |
| | | fout.close(); |
| | | fin.close(); |
| | | } |