fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace CourseWork_code
  9. {
  10. #region Ініціалізація головних змінних в проекті
  11. class Shared
  12. {
  13. public static int N = 6;
  14. public static int H = N / 2;
  15. public static int[] A;
  16. public static int[] B;
  17. public static int[] Z;
  18. public static int[,] MC;
  19. public static int[,] MD;
  20. public static int[,] CalcMO;
  21. public static int[] CalcMP;
  22.  
  23. public static Semaphore T1Data = new Semaphore(10, 10);
  24. public static Semaphore T2Data = new Semaphore(10, 10);
  25. public static Semaphore T3Data = new Semaphore(10, 10);
  26. public static Semaphore T4Data = new Semaphore(10, 10);
  27.  
  28. public static Semaphore T1Release = new Semaphore(10, 10);
  29. public static Semaphore T2Release = new Semaphore(10, 10);
  30. public static Semaphore T3Release = new Semaphore(10, 10);
  31. public static Semaphore T4Release = new Semaphore(10, 10);
  32.  
  33. public static Mutex T1Copy = new Mutex();
  34. public static Mutex T2Copy = new Mutex();
  35. public static Mutex T3Copy = new Mutex();
  36. public static Mutex T4Copy = new Mutex();
  37. }
  38. #endregion
  39.  
  40. class T1
  41. {
  42. public Thread thread_1;
  43. public T1(string name)
  44. {
  45. thread_1 = new Thread(new ThreadStart(this.Run));
  46. thread_1.Name = name;
  47. thread_1.Start();
  48. }
  49. void Run()
  50. {
  51. Console.Write("\nFirst thread started!");
  52.  
  53. Shared.T1Data.WaitOne();
  54.  
  55. #region Ініціалізація даних
  56. Shared.A = new int[Shared.N];
  57. Shared.Z = new int[Shared.N];
  58. for (int i = 0; i < Shared.N; i++)
  59. {
  60. Shared.A[i] = 0;
  61. Shared.Z[i] = 1;
  62. }
  63. #endregion
  64.  
  65. Console.Write("A = ");
  66. for (int i = 0; i < Shared.N; i++)
  67. {
  68. Console.Write($"{Shared.A[i]} ");
  69. }
  70.  
  71. Shared.T1Data.Release();
  72. Shared.T1Copy.WaitOne();
  73.  
  74. #region Копіювання даних
  75. int[] B1 = new int[Shared.N];
  76. for (int i = 0; i < Shared.N; i++)
  77. {
  78. B1[i] = Shared.B[i];
  79. }
  80. int[,] MC1 = new int[Shared.N, Shared.N];
  81. for (int i = 0; i < Shared.N; i++)
  82. {
  83. for (int j = 0; j < Shared.N; j++)
  84. {
  85. MC1[i, j] = Shared.MC[i, j];
  86. }
  87. }
  88. #endregion
  89.  
  90. Shared.T1Copy.ReleaseMutex();
  91. Shared.T1Release.WaitOne();
  92.  
  93. #region Обчислення задачі
  94. Shared.CalcMO = new int[Shared.N, Shared.N];
  95. for (int t = 0; t < Shared.N; t++)
  96. {
  97. for (int i = 0; i < Shared.N; i++)
  98. {
  99. for (int j = 0; j < Shared.N; j++)
  100. {
  101. Shared.CalcMO[i, j] += MC1[i, t] * Shared.MD[t, j];
  102. }
  103. }
  104. }
  105.  
  106. Shared.CalcMP = new int[Shared.N];
  107. for (int i = 0; i < Shared.N; i++)
  108. {
  109. for (int j = 0; j < Shared.N; j++)
  110. {
  111. Shared.CalcMP[i] += B1[i] * Shared.CalcMO[i, j];
  112. }
  113. }
  114.  
  115. for (int i = 0; i < Shared.N; i++)
  116. {
  117. for (int j = 0; j < Shared.N; j++)
  118. {
  119. Shared.A[i] = Shared.CalcMP[i] + Shared.Z[i];
  120. }
  121. }
  122. #endregion
  123.  
  124. Shared.T1Release.Release();
  125.  
  126. #region Вивід даних
  127. Console.Write("A = ");
  128. for (int i = 0; i < Shared.N; i++)
  129. {
  130. Console.Write($"{Shared.A[i]} ");
  131. }
  132.  
  133. Console.Write("\nB = ");
  134. for (int i = 0; i < Shared.N; i++)
  135. {
  136. Console.Write($"{Shared.B[i]} ");
  137. }
  138.  
  139. Console.WriteLine("\nMC = ");
  140. for (int i = 0; i < Shared.N; i++)
  141. {
  142. for (int j = 0; j < Shared.N; j++)
  143. {
  144. Console.Write($"{Shared.MC[i, j]} ");
  145. }
  146. Console.WriteLine();
  147. }
  148.  
  149. Console.WriteLine("MD = ");
  150. for (int i = 0; i < Shared.N; i++)
  151. {
  152. for (int j = 0; j < Shared.N; j++)
  153. {
  154. Console.Write($"{Shared.MD[i, j]} ");
  155. }
  156. Console.WriteLine();
  157. }
  158.  
  159. Console.WriteLine("CalcMO = ");
  160. for (int i = 0; i < Shared.N; i++)
  161. {
  162. for (int j = 0; j < Shared.N; j++)
  163. {
  164. Console.Write($"{Shared.CalcMO[i, j]} ");
  165. }
  166. Console.WriteLine();
  167. }
  168.  
  169. Console.Write("CalcMP = ");
  170. for (int i = 0; i < Shared.N; i++)
  171. {
  172. Console.Write($"{Shared.CalcMP[i]} ");
  173. }
  174. #endregion
  175.  
  176. Console.WriteLine("\nFirst thread finished!");
  177. }
  178. }
  179. class T2
  180. {
  181. public Thread thread_2;
  182. public T2(string name)
  183. {
  184. thread_2 = new Thread(new ThreadStart(this.Run));
  185. thread_2.Name = name;
  186. thread_2.Start();
  187. }
  188. void Run()
  189. {
  190. Console.Write("\nSecond thread started!");
  191.  
  192. Shared.T2Data.WaitOne();
  193.  
  194. #region Ініціалізація даних
  195. Shared.B = new int[Shared.N];
  196. for (int i = 0; i < Shared.N; i++)
  197. {
  198. Shared.B[i] = 1;
  199. }
  200.  
  201. Shared.MD = new int[Shared.N, Shared.N];
  202. for (int i = 0; i < Shared.N; i++)
  203. {
  204. for (int j = 0; j < Shared.N; j++)
  205. {
  206. Shared.MD[i, j] = 1;
  207. }
  208. }
  209. #endregion
  210.  
  211. Console.Write("\nB = \n");
  212. for (int i = 0; i < Shared.N; i++)
  213. {
  214. Console.Write($"{Shared.B[i]} ");
  215. }
  216.  
  217. Console.WriteLine("MD = \n");
  218. for (int i = 0; i < Shared.N; i++)
  219. {
  220. for (int j = 0; j < Shared.N; j++)
  221. {
  222. Console.Write($"{Shared.MD[i, j]} ");
  223. }
  224. Console.WriteLine();
  225. }
  226.  
  227. Shared.T2Data.Release();
  228. Shared.T2Copy.WaitOne();
  229.  
  230. #region Копіювання даних
  231. int[] B2 = new int[Shared.N];
  232. for (int i = 0; i < Shared.N; i++)
  233. {
  234. B2[i] = Shared.B[i];
  235. }
  236.  
  237. int[,] MC2 = new int[Shared.N, Shared.N];
  238. for (int i = 0; i < Shared.N; i++)
  239. {
  240. for (int j = 0; j < Shared.N; j++)
  241. {
  242. MC2[i, j] = Shared.MC[i, j];
  243. }
  244. }
  245. #endregion
  246.  
  247. Shared.T2Copy.ReleaseMutex();
  248. Shared.T2Release.WaitOne();
  249.  
  250. #region Обчислення задачі
  251. Shared.CalcMO = new int[Shared.N, Shared.N];
  252. for (int t = 0; t < Shared.N; t++)
  253. {
  254. for (int i = 0; i < Shared.N; i++)
  255. {
  256. for (int j = 0; j < Shared.N; j++)
  257. {
  258. Shared.CalcMO[i, j] += MC2[i, t] * Shared.MD[t, j];
  259. }
  260. }
  261. }
  262.  
  263. Shared.CalcMP = new int[Shared.N];
  264. for (int i = 0; i < Shared.N; i++)
  265. {
  266. for (int j = 0; j < Shared.N; j++)
  267. {
  268. Shared.CalcMP[i] += B2[i] * Shared.CalcMO[i, j];
  269. }
  270. }
  271.  
  272. for (int i = 0; i < Shared.N; i++)
  273. {
  274. for (int j = 0; j < Shared.N; j++)
  275. {
  276. Shared.A[i] = Shared.CalcMP[i] + Shared.Z[i];
  277. }
  278. }
  279. #endregion
  280.  
  281. Console.WriteLine("\nSecond thread finished!");
  282.  
  283. Shared.T2Release.Release();
  284. }
  285. }
  286.  
  287. class T3
  288. {
  289. public Thread thread_3;
  290. public T3(string name)
  291. {
  292. thread_3 = new Thread(new ThreadStart(this.Run));
  293. thread_3.Name = name;
  294. thread_3.Start();
  295. }
  296. void Run()
  297. {
  298. Shared.T3Copy.WaitOne();
  299.  
  300. Console.Write("\nThird thread started!");
  301.  
  302. #region Копіювання даних
  303. int[] B3 = new int[Shared.N];
  304. for (int i = 0; i < Shared.N; i++)
  305. {
  306. B3[i] = Shared.B[i];
  307. }
  308.  
  309. int[,] MC3 = new int[Shared.N, Shared.N];
  310. for (int i = 0; i < Shared.N; i++)
  311. {
  312. for (int j = 0; j < Shared.N; j++)
  313. {
  314. MC3[i, j] = Shared.MC[i, j];
  315. }
  316. }
  317. #endregion
  318.  
  319. Shared.T3Copy.ReleaseMutex();
  320. Shared.T3Release.WaitOne();
  321.  
  322. #region Обчислення задачі
  323. Shared.CalcMO = new int[Shared.N, Shared.N];
  324. for (int t = 0; t < Shared.N; t++)
  325. {
  326. for (int i = 0; i < Shared.N; i++)
  327. {
  328. for (int j = 0; j < Shared.N; j++)
  329. {
  330. Shared.CalcMO[i, j] += MC3[i, t] * Shared.MD[t, j];
  331. }
  332. }
  333. }
  334.  
  335. Shared.CalcMP = new int[Shared.N];
  336. for (int i = 0; i < Shared.N; i++)
  337. {
  338. for (int j = 0; j < Shared.N; j++)
  339. {
  340. Shared.CalcMP[i] += B3[i] * Shared.CalcMO[i, j];
  341. }
  342. }
  343.  
  344. for (int i = 0; i < Shared.N; i++)
  345. {
  346. for (int j = 0; j < Shared.N; j++)
  347. {
  348. Shared.A[i] = Shared.CalcMP[i] + Shared.Z[i];
  349. }
  350. }
  351. #endregion
  352.  
  353. Console.WriteLine("\nThird thread finished!");
  354.  
  355. Shared.T3Release.Release();
  356. }
  357. }
  358.  
  359. class T4
  360. {
  361. public Thread thread_4;
  362. public T4(string name)
  363. {
  364. thread_4 = new Thread(new ThreadStart(this.Run));
  365. thread_4.Name = name;
  366. thread_4.Start();
  367. }
  368. void Run()
  369. {
  370. Shared.T4Data.WaitOne();
  371.  
  372. Console.Write("\nFourth thread started!");
  373.  
  374. #region Ініціалізація даних
  375. Shared.MC = new int[Shared.N, Shared.N];
  376. for (int i = 0; i < Shared.N; i++)
  377. {
  378. for (int j = 0; j < Shared.N; j++)
  379. {
  380. Shared.MC[i, j] = 1;
  381. }
  382. }
  383. #endregion
  384.  
  385. Console.WriteLine("\nMC = \n");
  386. for (int i = 0; i < Shared.N; i++)
  387. {
  388. for (int j = 0; j < Shared.N; j++)
  389. {
  390. Console.Write($"{Shared.MC[i, j]} ");
  391. }
  392. Console.WriteLine();
  393. }
  394.  
  395. Shared.T4Data.Release();
  396. Shared.T4Copy.WaitOne();
  397.  
  398. #region Копіювання даних
  399. int[] B4 = new int[Shared.N];
  400. for (int i = 0; i < Shared.N; i++)
  401. {
  402. B4[i] = Shared.B[i];
  403. }
  404.  
  405. int[,] MC4 = new int[Shared.N, Shared.N];
  406. for (int i = 0; i < Shared.N; i++)
  407. {
  408. for (int j = 0; j < Shared.N; j++)
  409. {
  410. MC4[i, j] = Shared.MC[i, j];
  411. }
  412. }
  413. #endregion
  414.  
  415. Shared.T4Copy.ReleaseMutex();
  416. Shared.T4Release.WaitOne();
  417.  
  418. #region Обчислення задачі
  419. Shared.CalcMO = new int[Shared.N, Shared.N];
  420. for (int t = 0; t < Shared.N; t++)
  421. {
  422. for (int i = 0; i < Shared.N; i++)
  423. {
  424. for (int j = 0; j < Shared.N; j++)
  425. {
  426. Shared.CalcMO[i, j] += MC4[i, t] * Shared.MD[t, j];
  427. }
  428. }
  429. }
  430.  
  431. Shared.CalcMP = new int[Shared.N];
  432. for (int i = 0; i < Shared.N; i++)
  433. {
  434. for (int j = 0; j < Shared.N; j++)
  435. {
  436. Shared.CalcMP[i] += B4[i] * Shared.CalcMO[i, j];
  437. }
  438. }
  439.  
  440. for (int i = 0; i < Shared.N; i++)
  441. {
  442. for (int j = 0; j < Shared.N; j++)
  443. {
  444. Shared.A[i] = Shared.CalcMP[i] + Shared.Z[i];
  445. }
  446. }
  447. #endregion
  448.  
  449. Console.WriteLine("\nFourth thread finished!");
  450.  
  451. Shared.T4Release.Release();
  452. }
  453. }
  454. class Program
  455. {
  456. static void Main(string[] args)
  457. {
  458. Shared shared = new Shared();
  459. Console.WriteLine("Main started");
  460.  
  461. T1 one = new T1("first");
  462. T2 two = new T2("second");
  463. T3 three = new T3("third");
  464. T4 four = new T4("fourth");
  465. one.thread_1.Join();
  466. two.thread_2.Join();
  467. three.thread_3.Join();
  468. four.thread_4.Join();
  469.  
  470. Console.WriteLine("\nMain finished");
  471.  
  472. Console.ReadLine();
  473. }
  474. }
  475. }
Runtime error #stdin #stdout #stderr 0.04s 28580KB
stdin
Standard input is empty
stdout
Main started

First thread started!A = 0 0 0 0 0 0 
Second thread started!
B = 
1 1 1 1 1 1 MD = 

1 1 1 1 1 1 
1 1 1 1 1 1 
1 1 1 1 1 1 
1 1 1 1 1 1 
1 1 1 1 1 1 
1 1 1 1 1 1 
stderr
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at CourseWork_code.T1.Run () [0x000bc] in <39b3c591cca140b28cdf21e737698c60>:0 
  at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00014] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <6649516e5b3542319fb262b421af0adb>:0 

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at CourseWork_code.T2.Run () [0x00199] in <39b3c591cca140b28cdf21e737698c60>:0 
  at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00014] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <6649516e5b3542319fb262b421af0adb>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at CourseWork_code.T2.Run () [0x00199] in <39b3c591cca140b28cdf21e737698c60>:0 
  at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00014] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <6649516e5b3542319fb262b421af0adb>:0 
  at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <6649516e5b3542319fb262b421af0adb>:0